1 Target of URI doesn't exist 'package:flutter/material.dart'

官方下载的flutter中有一个example文件夹,里面有很多flutter的小demo,打开其中一个demo,执行运行的指令后,出现该错误。

错误原因是没有下载flutter依赖包,就像RN没有执行npm install 一样

解决方法:执行flutter packages get

2 flutter: command not found

flutter环境变量的配置

mac配置环境变量参考该网址https://jingyan.baidu.com/article/8065f87f47b29523312498e4.html

export PUB_HOSTED_URL=https://pub.flutter-io.cn

export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

export PATH="$PWD/flutter/bin:$PATH"

关闭终端,重新打开一次就可以了

具体可以参考https://flutter.dev/community/china

3 Error on line 45, column 4 of pubspec.yaml: Expected a key while parsing a block mapping

assets:
^

assets前面多了一个空格,删除就好了

4 More than one device connected; please specify a device with the '-d <deviceId>' flag, or use '-d all' to act on all devices.

ios和flutter混合开发,使用flutter attach可以调试flutter代码

但是运行flutter attach后并没有出现预期的效果,而是出现了

More than one device connected; please specify a device with the '-d <deviceId>' flag, or use '-d all' to act on all devices.

 

“xxx”的 iPhone • b13a6cf6ca28106dcbd0d6273c205d0fec8583aa • ios • iOS 12.1.2

iPhone X      • 108E3AF0-CFF1-4069-8989-0CF95B2EAD31     • ios • iOS 12.1 (simulator)

当时就仔细看第一行是啥意思,又上网查资料浪费的好长时间,感觉是个好低级的错误。

其实就是同时连接了2个设备,运行flutter attach -d <deviceId>选择其中一个就可以了,

或者断开一个设备,只连接一个设备就好了

 

5 A RenderFlex overflowed by 240 pixels on the right

渲染的部分超出屏幕右侧的宽度了

解决外层包裹一个可以滑动的widget,例如SingleChildScrollView,

因为我是水平方向超出了,所以还需要加上

scrollDirection: Axis.horizontal,
 
6 ListView运行时, 报错Vertical viewport was given unbounded height
解决
可以通过指定shrinkWrap = true为了你ListView
ListView(
  shrinkWrap: true,
  children...
)

可参考: https://cloud.tencent.com/developer/ask/138715

 

7 ScrollView嵌套ListView滚动冲突

解决: ListView添加physics:NeverScrollableScrollPhysics(),禁用ListView的滚动。

 
8 使用Android Studio调试项目
我们点击AS状态栏的flutter attach可以调试我们的项目(打断点,看变量等),当进入flutter页面时,需要等一段时间才能连接上我的的手机或模拟器,而网络请求部分在等待时间就请求完了,没有进入断点。
解决:在网络请求前加一个延时(
await Future.delayed(Duration(seconds: 6,));
),这样可以保证连接成功前还没有运行到我们的断电,成功后正常进入断点。
 
9 The element type 'Type' can't be assigned to the list type 'Widget'
body: TabBarView(
        controller: model._tabController,
        children: <Widget>[
          Content,
          Content
        ]
      )


class Content extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return Center(child: Text("测试"),);
  }

}

解决: 调用时加上小括号,将Content改成Content()就可以了

 10 dart中map方法获取index

参考: https://cloud.tencent.com/developer/ask/207919/answer/321886

List arr = ["a", "b", "c"];
List.asMap().map((index, item)=> MapEntry(index,Text(item)
)).values.toList()

11 当电脑删除原来的flutter sdk,然后在官网上下载最新的flutter版本后,建议重新更新环境变量,否则会有问题

 

12. 关于GestureDetector中onTap点击事件对空白处点击不相应的bug

GestureDetector(
  child: Container(
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Text("左"),
        Text("右")
      ],
    ),
  ),
  onTap: () {
    print("点击了Container");
  },
);

如上代码所示,当点击文字“左”“右”时会触发点击时间,点击其他地方并不会触发点击事件,如果想点击其他空白处也触发点击事件,可以在Contaier加一个color属性,如下代码所示 

GestureDetector(
  child: Container(
    color: Colors.transparent,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Text("左"),
        Text("右")
      ],
    ),
  ),
  onTap: () {
    print("点击了Container");
  },
);

 13 由于本地flutter环境与服务器打包flutter环境不一致导致连续点击2次flutter页面崩溃的问题

原来使用的flutter版本为1.2.1,之后升级了flutter版本到1.5.4,对应pubspec.lock中对应的依赖版本有变化

只是本地升级,服务器环境没有升级,所以打包时环境不对,出现了连续点击2次flutter页面导致崩溃,

升级服务器的flutter环境后,问题解决

 

14 pageView放在stack里,而此时stack是column的子组件,容易出错,将放置顺序调整为column放在stack里面,pageView放在column中没有问题

需要主要stack和colum的放置顺序

 

15  Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.

这个问题找解决方案找了很长时间

终于在这个网站https://stackoverflow.com/questions/52296387/scrollcontroller-jumpto-scrollcontroller-not-attached-to-any-scroll-views看到解决的要点

问题是出在

_pageController.animateToPage(
        _index, 
        duration: changeTimeOut, 
        curve: Curves.linear
      );

 我使用了上面的代码,但是此时_pageController是没有的(向上滑动后这部分UI不显示了,所以flutter就不渲染了,我理解_pageController也就不存在了),所以报错了。

解决方案是加一个判断,等向上滑动后UI不显示了,就不执行这部分代码就好了

上面那个网站给出了是 if (_scrollController.hasClients){}

对应我的代码的解决方案是if (_scrollController.offset < 200) {上面的代码}

16 输入flutter attach时出现以下提示 

Waiting for another flutter command to release the startup lock...

解决:

此时需要打开 flutter/bin/cache/lockfile,删除就行了

参考:https://www.jianshu.com/p/7507d087e9f1

 

17 更新flutter版本1.7.8+hotfix.3后出现以下错误

Error: The argument type 'Null Function(ImageInfo, bool)' can't be assigned to the parameter type 'ImageStreamListener'.

 

解决方法: 

将出现错误的地方

_imageStream.addListener(_handleImageLoaded);

改成

_imageStream.addListener(ImageStreamListener(_handleImageLoaded));

 

18 [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0 

[ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size!

原因是引用的字体或者图标不存在

 

19 [FATAL:flutter/runtime/dart_vm.cc(380)] Error while initializing the Dart VM: Wrong full snapshot version, expected '1d7acad1540192ac459cf60344efb7c1' found 'c8562f0ee0ebc38ba217c7955956d1cb'

问题发生于升级flutter sdk之后,看上边的日志Wrong full snapshot version可以猜测:升级了sdk,但是之前已生成的编译产物还是旧的,不匹配,需要重新build一下

1) removing the content of flutter/bin/cache
2) thenrunning flutter upgrade again
3) thenrunning flutter clean prior to flutter run
参考 https://juejin.im/post/5d1c596d6fb9a07ee742fa59
 
20 flutter升级到1.9.1 后 ios打包失败 Flutter.framework: Permission denied
 
这是flutter官方的一个bug,查资料说flutter升级到1.10就好了
不升级flutter的情况下,可以改Flutter SDK 的一个文件,flutter/packages/flutter_tools/bin/xcode_backend.sh
144行

RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -exec chmod a-w "{}" \;

=>

RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -iname '.h' -exec chmod a-w "{}" \;

 21 

E/flutter_error: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞══════════════════════
The following NoSuchMethodError was thrown during paint():
The method '>' was called on null.
Receiver: null
Tried calling: >(1e-10)

错误代码:

Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          height: 40,
          width: 100
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: ...
        )
         
        topicListMainUIWidget()
      ],
    )        

错误原因是因为Row里是一个list,高度是不固定的,需要一个Expanded Widget把Row包裹起来