flutter-开发问题系列之十三   2021-12-25


这是 todoline++ 项目开发过程中遇到的问题系列, 记录下来供大家参考。
本篇记录一些开发中遇到的报错:

todoline++ 官网请看这里: https://todoline-plus-plus.github.io/

1 Unhandled Exception: Unable to load asset: assets//data/user/0/com.github.todoline.plus.plus.tracker/cache/file_picker/神奇的九寨

插件 audioplayers 有两个类可以播放音乐, AudioCache 和 AudioPlayer, AudioCache 播放音乐默认文件路径在 asserts 下面, 所以报错。
使用 AudioPlayer 来播放就可以了。

2 Unhandled Exception: type ‘Null’ is not a subtype of type ‘int’ in type cast

这是因为数据为空, 将 Null 转化为 int 导致报错.
那为什么会有 Null 呢? 追踪报错, 发现错误是 floor 自动构建的代码中, 把 create_time 转换成 int 时报错, 代码如下:

1
2
3
4
5
6
7
@override
Future<List<Todo>?> listTodo() async {
return _queryAdapter.queryList('SELECT * FROM v_t where deleted = 0',
mapper: (Map<String, Object?> row) => Todo(
row['id'] as int?,
row['create_time'] as int);
}

floor 从数据库查询 create_time 时, 数据为空, 转换成 int 时就报错了! 因为 create_time 不可为空, 那为什么 floor 构建后的代码中 create_time 不可为空呢? 我如何才能让他允许为空? 进一步 google, 有人提议将映射的实体类的成员变量设置可为空就可以了. 我查看了下对应的成员变量, 确实可为空, 那为什么 floor 构建代码时为什么不可为空呢? 百思不得其解, 准备要查看 floor 源代码来确认怎么回事, 突然猜测到, 有可能是 floor 根据构造器来决定成员变量是否可为空, 果然, 修改后 floor 构建的代码正常.

3 Unhandled Exception: Null check operator used on a null value

App.logger.d(“finished todo: ${_handlingTodo?.toMap()}”);
_handlingTodo 为 null, 所以报错。

4 Unhandled Exception: type ‘MaterialPageRoute dynamic’ is not a subtype of type ‘Route Object?’ in type cast

Navigator.pushNamed 不能使用 await 来等待返回, 错误代码如下:

1
2
3
4
void toRecord(BuildContext context) async {
Object? mood = await Navigator.pushNamed(context, "/MoodAddView");
// Next To Do ...
}

正确代码如下:

1
2
3
4
5
void toRecordMood(BuildContext context) {
Navigator.pushNamed(context, "/MoodAddView").then((mood){
// Next To Do ...
});
}

5 AAPT: error: adaptive-icon elements require a sdk version of at least 26.

android 资源目录下删除掉原来的 launch_background.xml 文件, 自己多加了一个 ic_launcher.xml 文件引起

launch_background.xml 文件内容:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?android:colorBackground" />
</layer-list>

ic_launcher.xml 文件内容:

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

这可能是 adaptive-icon 最低版本需要 26 才可以。

6 Don’t put any logic in createState

当 createState 方法中调用 state 页面时, 传入太多的参数就会包此错误, 我们完全可以使用 widget.xxx 来调用 StatefulWidget 类的成员变量, 不需要再传递。

7 SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero lengt

不知道该怎么办

8 Invalid argument(s): Illegal argument in isolate message: (object is aReceivePort)

SendPort.send 发生错误, 原因: Isolate 在独立的环境中运行, 这意味着参数传递时只能传原始类型的数据, 自定义参数类型将不被允许。

建议使用 easy_isolate 代替, 它将解决这个问题: pub.dev/packages/easy_isolate

来源:
stackoverflow.com/questions/67897395/invalid-arguments-illegal-argument-in-isolate-message-in-flutter

9 StatefulWidget.createState must return a subtype of State Inbox

我们创建的页面布局文件, class 类必须继承自 State 类, 自己需要写一个继承页面,以便于达到封装的目的 , 不知道, 写错了, 所以出现这个报错。

10 Could not find any matches for com.transistorsoft:tsbackgroundfetch:+ as no versions of com.transistorsoft:tsbackgroundfetch are available.

tsbackgroundfetch 似乎有问题, 加载的版本是 0.7.3 或者更小的话, 不支持 null-safety, 加载版本大于 0.7.3 的话, 就报此错误。


分享到:


  如果您觉得这篇文章对您的学习很有帮助, 请您也分享它, 让它能再次帮助到更多的需要学习的人. 您的支持将鼓励我继续创作 !
本文基于署名4.0国际许可协议发布,转载请保留本文署名和文章链接。 如您有任何授权方面的协商,请邮件联系我。

Contents

  1. 1 Unhandled Exception: Unable to load asset: assets//data/user/0/com.github.todoline.plus.plus.tracker/cache/file_picker/神奇的九寨
  2. 2 Unhandled Exception: type ‘Null’ is not a subtype of type ‘int’ in type cast
  3. 3 Unhandled Exception: Null check operator used on a null value
  4. 4 Unhandled Exception: type ‘MaterialPageRoute dynamic’ is not a subtype of type ‘Route Object?’ in type cast
  5. 5 AAPT: error: adaptive-icon elements require a sdk version of at least 26.
  6. 6 Don’t put any logic in createState
  7. 7 SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero lengt
  8. 8 Invalid argument(s): Illegal argument in isolate message: (object is aReceivePort)
  9. 9 StatefulWidget.createState must return a subtype of State Inbox
  10. 10 Could not find any matches for com.transistorsoft:tsbackgroundfetch:+ as no versions of com.transistorsoft:tsbackgroundfetch are available.