Flutter常见错误

2019-03-05  本文已影响0人  夏_Leon

记录Flutter学习过程中碰到比较常见的坑,持续更新。

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)

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),
            ),
          ],
        ),

这样就可以实现四个按钮均布了,实际上添加再多的按钮它也是均布不超出,就是每个按钮都被挤压得宽度只剩一点了。

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 []}
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的开发证书即可。

/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/下的文件清空了,重新获取。

  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.

Flutter之AndroidX相关问题

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。

上一篇 下一篇

猜你喜欢

热点阅读