Flutter⾛过的坑(持续更新)
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环境变量的配置
export PATH="$PWD/flutter/bin:$PATH"
关闭终端,重新打开⼀次就可以了
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,
<
)
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
List arr = ["a", "b", "c"];
List.asMap().map((index, item)=> MapEntry(index,Text(item)
)).List()
11 当电脑删除原来的flutter sdk,然后在官⽹上下载最新的flutter版本后,建议重新更新环境变量,否则会有问题
12. 关于GestureDetector中onTap点击事件对空⽩处点击不相应的bug
GestureDetector(
flutter sdkchild: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text("左"),
Text("右")
],
),
),
onTap: () {
print("点击了Container");
},
);
如上代码所⽰,当点击⽂字“左”“右”时会触发点击时间,点击其他地⽅并不会触发点击事件,如果想点击其他空⽩处也触发点击事件,可以在Contaier加⼀个color属性,如下代码所⽰
GestureDetector(
child: Container(
color: ansparent,
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.
这个问题解决⽅案了很长时间
问题是出在
_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
解决:
此时需要打开 flutter/bin/cache/lockfile,删除就⾏了
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/(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
参考 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包裹起来

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。