Flutter采坑記錄

在 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}");

比如說這種情況,如果userModelnull,后面使用了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.png

05.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- Run flutter 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.png
12.png

13.png
14.png

至此,這個問題成功解決,耗費了我兩天的時長,都是淚~??

bug 持續采坑中....
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,656評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,697評論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,098評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,855評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,254評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,473評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,014評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,833評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,016評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,273評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,680評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,946評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,730評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,006評論 2 374