在 Flutter 開發中遇到的一些 BUG,避免遺忘,記錄一下,如果正在看文章的你也遇到了,那激動的心,顫抖的手,咱們可以握個手。
1.Suggestion:use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 19,1.png
解決方案
2.png將minsdk提升到19以上
2.flutter打包出來界面空白
這個問題,困擾我兩三天了,網上找了千萬種方法,無非以下三種情況:
① 升級到flutter SDK stable版本
② flutter clean 并且 flutter build ios --release,最后archive
③更換bundleId
但是都沒有卵用
最終我的原因是因為這個界面log有個如下的輸出error信息
Incorrect use of ParentDataWidget.
出現這個錯誤的原因主要是因為Expanded嵌套Column出錯了,如下面截圖所示:
001.png解決掉這個錯誤,打包白屏問題即解決,困擾了兩天的問題終于解決了。
Incorrect use of ParentDataWidget
3.
Looking up a deactivated widget's ancestor is unsafe.
這個錯誤的原因主要是因為當前的widget的context是null
StyleConfig styleConfig = Provider.of<StyleConfig>(context);
當前界面如果有類似(context)代碼,好好檢查下(context)是否為空。
4.
Failed assertion: line 826 pos 14: 'file != null': is not true.
這個問題,我是在拍照選擇的時候出現的,模擬器中點擊拍照功能,因為模擬器沒有攝像頭,所以報錯,主要原因還是因為代碼不嚴謹的緣故,造成image == null.
原代碼為:
Future getImage(bool isTakePhoto) async {
Navigator.pop(context);
var image = await ImagePicker.pickImage(
source:isTakePhoto ? ImageSource.camera :ImageSource.gallery);
setState(() {
// _image = image;
_images.add(image);
});
}
修改后代碼為:
Navigator.pop(context);
try {
var image = await ImagePicker.pickImage(
source:isTakePhoto ? ImageSource.camera :ImageSource.gallery
);
if (image == null) {
return;
} else {
setState(() {
// _image = image;
_images.add(image);
});
}
} catch (e) {
print("模擬器不支持相機!");
}
}
5.
The getter 'id' was called on null.
Receiver: null
Tried calling: id
這個錯誤,主要是因為模型為空,然后取值模型中的字段造成的,或者是因為字段類型不匹配,需要仔細檢查,別無它發。
UserModel userModel = Provider.of<UserModel>(context, >listen: false); print("用戶ID: ${userModel.user.id}");
比如說這種情況,如果
userModel
是null
,后面使用了userModel.user.id則會報這種錯誤。
6.
Row's children must not contain any null values, but a null value was found at index 0
01.png這種錯誤一般是因為Row里面為空造成的,比如項目開發中以下代碼就出現過相同的錯誤:
02.png
03.png
上面雖然調用了_buildBottomItem方法,但是這個方法內部并沒有返回一個widget,所以就報了上述的錯誤。
修改方法很簡單,直接在_buildBottomItem方法中return Container();即可解決。//解決方法: Widget _buildBottomItem(BottomItemType type) { // 寫自己的業務邏輯,或return一個默認的Container() return Container(); }
7.
Failed assertion: line 110 pos 12: '_positions.isNotEmpty'
如下圖所示:
03.png
項目中我的主要是因為在SingleChildScrollView里面嵌套了Swiper,,進行swiper count 判斷,如果數量為空則不顯示,數量不為空在顯示 if (null == _swipers || _swipers.isEmpty)
我的解決方案如下
04.png05.png
8.
Looking up a deactivated widget's ancestor is unsafe.
該問題是點擊返回的時候報錯,意思是不是安全的引用小部件,就是已經銷毀的界面然后重復銷毀,會爆如下錯誤,錯誤信息如下:
06.PNG
根據控制臺的錯誤信息,可以定位到是dispose方法報錯了,將FocusScope.of(context).requestFocus(blankFocus);
注釋掉即可解決。
9.
RangeError (index): Invalid value: Only valid value is 0: 1
這個報錯主要是因為在創建ListView視圖時,漏寫itemCount,或者itemCount==null造成的。
10.
The getter 'length' was called on null.
Receiver: null
Tried calling: length
這個報錯主要是因為某個字段為空造成的,可能數組空,可能走個字段空,都會引起該問題,一定要仔細排查每個字段,別無他法,同時代碼寫的要健壯一些,不要碰到null就直接拋錯誤了。例:項目中遇到的報錯情況如下:
07.png
所以代碼需要改為如下,即可消除掉這個報錯。
child:Text(formatTime(widget.question.time??""),
style: TextStyle(fontSize: 11,color: infoColor,),),
11.
Failed assertion: line 236 pos 16: 'controller != null': is not true.
這個bug是因為用了SmartRefresher控件,但是并沒有寫controller造成的,如下列所示的錯誤寫法:
return SmartRefresher(
// controller:_refreshController,
enableTwoLevel:false ,
enablePullDown: true,
header: WaterDropHeader(),
footer: null,
onRefresh: () async {
await pageModel.refresh();
pageModel.showErrorMessage(context);
},
onLoading: null,
enablePullUp: false,
child: Container(),
);
正確的寫法應該是必須有 controller: _refreshController.
12.
BUILD FAILED in 36s Exception: Gradle task assembleDebug failed with exit code 1.
詳細錯誤信息如下:
Launching lib/main.dart on V1818CT in debug mode...
Running Gradle task 'assembleDebug'...
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not resolve com.mob.sdk:MobSDK:+.
Required by:
project :
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/public/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/jcenter/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/jcenter/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/jcenter/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from http://maven.aliyun.com/nexus/content/groups/public/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'http://maven.aliyun.com/nexus/content/groups/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'http://maven.aliyun.com/nexus/content/groups/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from http://mvn.mob.com/android/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'http://mvn.mob.com/android/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'http://mvn.mob.com/android/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/google/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/google/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/google/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/central/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/central/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/central/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from http://download.flutter.io/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'http://download.flutter.io/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'http://download.flutter.io/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 36s
Exception: Gradle task assembleDebug failed with exit code 1
13. fatal:could not username for 'xxxxxx'
08.png
這個問題不是flutter代碼的問題,最主要原因可能是本地緩存的Git庫的賬號名和密碼賬號有問題,參考之前 HTTP Basic: Access denied fatal: Authentication failed...文章的處理方案即可解決。
14.versions is 9.0 to 14.0.99. (in target 'UMCCommon' from project 'Pods')
warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99. (in target 'mob_sharesdk' from project 'Pods')Could not build the application for the simulator.
Error launching application on iPhone 11 Pro Max.方案一:
post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) end end
Podfile文件中將這段代碼,替換為下面這段:
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| >config.build_settings['IPHONEOS_DEPLOYMENT_T>ARGET'] = '9.0' end flutter_additional_ios_build_settings(target) end end
方案二
將Build Settings最后一行的VALID_ARCHS這一行刪除,以為這里么沒有支持模擬器。09.png
15.
Could not build the application for the simulator..
Error launching application on iPhone 11 Pro Max..
10.png網上查閱很多資料,包括Stack Overflow說的都是clean,方案有多種,如下:
網上方案一:
Please follow these steps/run commands
flutter clean (in terminal)
flutter build (in terminal)
In Xcode, then clean project
In Xcode, then build project網上方案二
1- Open xcode for the iOS project by clicking on Runner.xcworkspacefile located in iOS directory
2- Click Runner (on the left of the Xcode)
3- Click on Build Settings tab (in the middle of Xcode)
4- Change iOS Deployment Target to 12.1 for example
5- Save your action
6- Runflutter clean
then run your app網上方案三:
rm iOS/Podfile
Then upgrade your packages:
pub upgrade
pub run
And update your podfile:
cd ios && pod update
Then clean and run:
flutter clean && flutter run網上方案四:
解決方法在as的Terminal里面
1.保證在下面根目錄下執行下面: flutter clean
2.然后cd到ios目錄執行下面: cd iOS
3.最后執行這一步: pod install
4.運行ios模擬器網上方案五:
1、關閉Xcode,打開終端:
2、進入DerivedData目錄
cd ~/Library/Developer/Xcode/DerivedData/
3、然后再終端執行:
xattr -rc .
4、再運行flutter項目,完美解決但是網上方案千千萬,沒有一個解決了我的問題,最后我的方案是:
將本地所有的庫全部刪除,重新pub get新的庫,即可解決問題,這個可能是庫緩存錯亂了,出現的錯誤。
解決步驟如下:11.png12.png
13.png
14.png
至此,這個問題成功解決,耗費了我兩天的時長,都是淚~??