记录Flutter学习过程中碰到比较常见的坑,持续更新。
- setState() or markNeedsBuild() called during build.
报错信息如下:
I/flutter (16998): setState() or markNeedsBuild() called during build.
I/flutter (16998): This ParentWidget widget cannot be marked as needing to build because the framework is already in
I/flutter (16998): the process of building widgets. A widget can be marked as needing to be built during the build
I/flutter (16998): phase only if one of its ancestors is currently building. This exception is allowed because the
I/flutter (16998): framework builds parent widgets before children, which means a dirty descendant will always be
I/flutter (16998): built. Otherwise, the framework might not visit this widget during this build phase.
原代码:
class MyButton extends StatefulWidget {
Function callback;
var btnColor;
MyButton(this.callback, this.btnColor);
@override
State createState() {
return _MyButton();
}
}
class _MyButton extends State<MyButton> {
@override
Widget build(BuildContext context) {
return RaisedButton(
//State里可以通过widget访问其StatefulWidget里的变量
onPressed: widget.callback(Colors.black12),
child: Text('Button2,点击修改父控件背景色'),
color: widget.btnColor,
);
}
}
报错原因是在自定义组件MyButton中传入了一个方法,赋值给callback,在onPressed中不能直接调用widget.callback(Colors.black12)
而应该使用()=> widget.callback(Colors.black12)
-
Waiting for another flutter command to release the startup lock...
在编译或运行命令的时候出现这种报错,最简单的方式就是重启AndroidStudio。 -
A RenderFlex overflowed by 12 pixels on the right
这是因为组件大小超过屏幕范围了,我源代码里这是一个Row(),宽度超过屏幕,有两种解决办法:
1、你实际需要的组件就是宽度超过屏幕的话,那就需要用一个scroll包裹,用SingleChildScrollView包裹下这个Row(),因为我这是水平超出范围,要设置scrollDirection: Axis.horizontal,
2、你实际需要的组件并不需要超过屏幕,那就是子组件的宽度设置错误,在初学Row的时候很容易遇到,我在一个Row中放入四个按钮就报错,报错代码:
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new RaisedButton(
textColor: Colors.black,
child: new Text('1'),
),
new RaisedButton(
textColor: Colors.black,
child: new Text('2'),
),
new RaisedButton(
textColor: Colors.black,
child: new Text('3'),
),
new RaisedButton(
textColor: Colors.black,
child: new Text('4'),
),
],
),
而我实际想要的是这四个按钮平均分布在一行里,这里就需要把子组件用容器包裹,有些还需要指定它大小的分配方式:
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Flexible(
child: new RaisedButton(
textColor: Colors.black, child: Text('增'), onPressed: _add),
),
new Flexible(
child: new RaisedButton(
textColor: Colors.black,
child: new Text('删'),
onPressed: _delete),
),
new Flexible(
child: new RaisedButton(
textColor: Colors.black,
child: new Text('改'),
onPressed: _update),
),
new Flexible(
child: new RaisedButton(
textColor: Colors.black,
child: new Text('查'),
onPressed: _query),
),
],
),
这样就可以实现四个按钮均布了,实际上添加再多的按钮它也是均布不超出,就是每个按钮都被挤压得宽度只剩一点了。
-
MissingPluginException(No implementation found for method showToast on channel Ponnam Karthik/fluttertoast)
项目中初次引入fluttertoast的时候报错,重新run编译整个项目,而不是仅热重启。 -
This version of path_provider will break your Android build if it or its dependencies aren't compatible with AndroidX.
在yaml文件导入最新版的fluttertoast依赖fluttertoast: ^3.0.1
的时候报错path_provider库不兼容AndroidX,而把toast的版本降低,比如fluttertoast: ^2.1.1
后又正常了,这个报错的地方非常奇怪。。。 -
创建数据库的时候 disk I/O error
flutter: dbPath:/var/mobile/Containers/Data/Application/443CF564-CA8A-4354-BFDA-A17B4790CAA4/Documents
DB Error: 10 "disk I/O error"
DB Query: CREATE TABLE user_table (id INTEGER PRIMARY KEY, username TEXT,pwd Text)
DB Path: /var/mobile/Containers/Data/Application/443CF564-CA8A-4354-BFDA-A17B4790CAA4/Documents/user.db
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
DatabaseException(Error Domain=FMDatabase Code=10 "disk I/O error" UserInfo={NSLocalizedDescription=disk I/O error}) sql 'CREATE TABLE user_table (id INTEGER PRIMARY KEY, username TEXT,pwd Text)' args []}
- iOS系统上,卸载再重装app报错,无法打开
Could not install build/ios/iphoneos/Runner.app on 81d01af8df61a458d021c2bf12fc092b87580b72.
Try launching Xcode and selecting "Product > Run" to fix the problem:
open ios/Runner.xcworkspace
再次信任App的开发证书即可。
- 程序包androidx.annotation不存在import androidx.annotation.VisibleForTesting;
在适配AndroidX失败后降级回来,发现报错
/Users/youdongzhen/Documents/soft/flutter/.pub-cache/hosted/pub.flutter-io.cn/image_picker-0.4.12+1/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerPlugin.java:8: 错误: 程序包androidx.annotation不存在
import androidx.annotation.VisibleForTesting;
按理解,应该是获取依赖的时候先去.pub-cache文件夹下面找,而缓存里的插件在之前升级AndroidX的时候被转换了,我就把.pub-cache/hosted/下的文件清空了,重新获取。
- 编译的时候报错 error: resource android:attr/dialogCornerRadius not found.
Output: /Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
/Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values-v28/values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
/Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:290: error: resource android:attr/fontVariationSettings not found.
/Users/youdongzhen/Documents/repository/trackandroid/flutter/flutter_app/build/app/intermediates/incremental/mergeDebugResources/merged.dir/values/values.xml:290: error: resource android:attr/ttcIndex not found.
error: failed linking references.
- drawArc绘制弧度时无法显示
canvas.drawArc(
rect,
-3.14 / 2,//起始弧度,从圆顶部开始绘制,起点应为-90°,转换为弧度为-π/2
progress / 100 * 360 * pai / 180,//终止弧度,100制进度转为弧度
false,//是否连线到中心点
_paintFore
..strokeWidth = borderWidth
..color = Colors.red
..style = PaintingStyle.stroke //绘画风格改为stroke
);
没有任何报错,就是绘制一段弧线无法显示,然而把其中的useCenter改为true,就能显示这段弧线了!目前已知该bug发生在V1.0.0的Flutter,并且只在真机调试中出问题,ios模拟器能正常工作,解决办法就是升级Flutter。
网友评论