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阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335