Flutter实际开发bug总结

目录

1.1 安卓release包缺少libflutter.so

1.2 AndroidStudio导入项目后自动变为model,没有Flutter目录

1.3 输入框内容为空时,长按不显示粘贴工具栏

1.4 SWIFT_VERSION '5.0' is unsupported, supported versions are: 3.0, 4.0, 4.2

1.5 复制粘贴面板英文的问题

1.6 调用库的时候报Methods marked with @UiThread must be executed on the main thread.Current thread: XXXX

1.7 用Navigator.of(context).pushNamed(routeName)如何传递参数

1.8 监听NestedScrollViewbody属性里的widget滚动事件

1.9 使用FutureBuilder每调用一次setState就会重新请求future

1.10 IOS上出现cutButtonLabel was called on null

1.11 IOS上打包后出现could not create Dart VM

1.12 SliverList里的内容无法定位

1.13 添加WillPopScope之后,IOS的滑动返回效果失效

1.14 ios端运行报错 not found

1.15 发布Flutter插件时出现Failed to upload the package \n pub finished with exit code 1
1.16 慎用GlobalKey,拿取currentState时有一个for循环,页面多时会出现卡顿

1.1 安卓release包缺少libflutter.so

修改/android/app/build.gradle文件如下

android{
    defaultConfig{
        ndk {
            abiFilters "armeabi-v7a", "x86"
       }
    }
    buildTypes {
          debug {
              ndk {
                abiFilters "armeabi-v7a", "x86"
              }
          }
          release {
              ndk {
                 abiFilters "armeabi-v7a"
              }   
         }
    }
}

1.2 AndroidStudio导入项目后自动变为model,没有Flutter目录

Flutter实际开发bug总结_第1张图片
image.png

解决方法:
在导入项目时选择下面选项
File-Open-选中你的项目

1.3 输入框内容为空时,长按不显示粘贴工具栏

将输入框中的autoFocus属性为ture去掉

1.4 SWIFT_VERSION '5.0' is unsupported, supported versions are: 3.0, 4.0, 4.2

将项目打开为ios项目,然后在文件列面中找到Pods(建议升级xcode即可解决)

Flutter实际开发bug总结_第2张图片
image.png

1.5 复制粘贴面板英文的问题

pubspec.yaml添加国际化支持,然后运行flutter packages get

dependencies:
  ...
  flutter_localizations:
    sdk: flutter

找到代码MaterialApp或者CupertinoApp或者WidgetsApp的文件,添加下面代码即可

        MaterialApp(
//...
//new
                localizationsDelegates: const [
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate
                ],
                supportedLocales:[
                  Locale('zh',''),
                  Locale('en','')
                ],
//new
              )

1.6 调用库的时候报Methods marked with @UiThread must be executed on the main thread.Current thread: XXXX

出现该异常的主要原因是Flutter1.7.8版本添加了线程安全,需要原生在主线程中返回给Flutter
解决方法:

  • 库的问题?
    到pub库中找到最新的版本,更改最新的版本,然后运行flutter packages get
  • 自己写的库问题?
    假如:
//Result result  flutter的result
new Thread(new Runnable() {
      public void run() {
//.....
result.success(null);//这里就会导致异常
     }).start();

改为

//Result result  flutter的result
new Thread(new Runnable() {
      public void run() {
//.....
    new Handler().post(new Runnable() {
                @Override
                public void run() {
                    result.success(file.getAbsolutePath());
                }
            });
     }).start();

上面是伪代码,不建议这样做,可能会导致内存溢出

1.7 用Navigator.of(context).pushNamed(routeName)如何传递参数

传递参数

Navigator.of(context).pushNamed(routeName,arguments:{
  “name":"我是参数"
})

获取参数

final arguments=ModalRoute.of(context).settings.arguments;

1.8 监听NestedScrollViewbody属性里的widget滚动事件

这个只要在对应的body里嵌套NotificationListener即可

        NestedScrollView(
//          controller: scrollController,
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return [
              //....
            ];
          },
          body: Builder(
              builder: (BuildContext context) => NotificationListener(
                   onNotification: onNotification,//你要的监听
                   child: CustomScrollView(
                         slivers: [
                           //....
                         ],
                  ),
            ),
        )

1.9 使用FutureBuilder每调用一次setState就会重新请求future

解决方法:将future提取出来,作为一个变量

Future future;

  @override
  void initState() {
    super.initState();
    future=getInt();
  }

  FutureBuilder(
    future: future,
    builder: (context, snapshot) {
      return ...;
    }
  ),

  Future getInt(){
    return Future.value(1);

  }

1.10 IOS上出现cutButtonLabel was called on null

解决方法:添加GlobalCupertinoLocalizations.delegate

MaterialApp(
//...
                localizationsDelegates: [
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
//new
                  GlobalCupertinoLocalizations.delegate,
//new 
                ],
                supportedLocales: [
                  const Locale('zh', 'CH'),
                  const Locale('en', 'US'),
                ],
                locale: const Locale('zh'),
//...
              )

1.11 IOS上打包后出现could not create Dart VM

因为你没有生成flutter_assets相关文件,所以导致Dart代码没有打进包里

解决方法:在打包前运行一下命令,生成的文件不用管,继续你的操作

flutter build ios --release

1.12 SliverList里的内容无法定位

将需要定位的内容单独移出来即可

1.13 添加WillPopScope之后,IOS的滑动返回效果失效

WillPopScope去掉即可

1.14 ios端运行报错 not found

将/ios/Podfile.lock 文件删除后到ios目录下重新运行命令pod install即可

1.15 发布Flutter插件时出现Failed to upload the package \n pub finished with exit code 1

因为你设置了PUB_HOSTED_URL导致上传到了其他的pub仓库中,dart主要的仓库地址为https://pub.dev只要设置成为该地址即可

Flutter实际开发bug总结_第3张图片
image.png

欢迎在评论区留下你的bug问题,在线修bug

你可能感兴趣的:(Flutter实际开发bug总结)