错误记录Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )(代

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误记录Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )(代相关的知识,希望对你有一定的参考价值。





一、报错信息



打开了一个去年写的 Flutter 项目 , 发现直接报错 ;

The parameter 'icon' can't have a value of 'null' because of its type, 
but the implicit default value is 'null'. 
(Documentation)  Try adding either an explicit non-'null' default value or the 'required' modifier.





二、解决方案



这是由于 Flutter SDK 版本升级 , 对语法的检查变得更加严格 , 在编译过程中对构造方法中的类添加了空判断 ;

在 Dart 类的成员变量定义时 , 在类型后面添加 ? 符号 , 将其声明为可空类型 , 与 Kotlin 类似 , 修改后 , 编译不再报错 ;

class CommonModel {
  final String? icon;
  final String? title;
  final String? url;
  final String? statusBarColor;
  final bool? hideAppBar;

  CommonModel({this.icon, this.title, this.url, this.statusBarColor, this.hideAppBar});
}

以上是关于错误记录Flutter 编译报错 ( The parameter ‘‘ can‘t have a value of ‘null‘ because of its type, but the im )(代的主要内容,如果未能解决你的问题,请参考以下文章

错误记录编译 Linux 内核报错 ( Unable to find the ncurses package. )

错误记录Android Studio 编译报错 ( AppCompat does not support the current theme features )

错误记录Android 编译报错 ( The minCompileSdk (31) specified in a dependency‘s AAR metadata (META-INF/com )

错误记录Android Studio 编译报错 ( The dependency contains Java 8 bytecode. Please enable desugaring by )

错误记录Android Studio 编译报错 ( The minCompileSdk (31) specified in a dependency‘s AAR metadata (META- )

错误记录Android Studio 编译报错 ( The minCompileSdk (33) specified in a dependency‘s AAR metadata META-I )