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

简介: 【错误记录】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.


image.png





二、解决方案


这是由于 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});
}


目录
打赏
0
0
0
0
39
分享
相关文章
|
7月前
|
API
Flutter 解决Type ‘MouseCursor‘ not found.
Flutter 解决Type ‘MouseCursor‘ not found.
67 0
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
220 4
|
7月前
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
275 3
【错误记录】发布 Flutter 插件包报错 ( It‘s strongly recommended to include a “homepage“ or “repository“ field )
【错误记录】发布 Flutter 插件包报错 ( It‘s strongly recommended to include a “homepage“ or “repository“ field )
267 0
【错误记录】发布 Flutter 插件包报错 ( It‘s strongly recommended to include a “homepage“ or “repository“ field )
消除Flutter doctor的警告Warning: `dart` on your path resolves to xxx/bin/dart
消除Flutter doctor的警告Warning: `dart` on your path resolves to xxx/bin/dart
142 0
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
|
10月前
|
CMake中的find_package(xxx REQUIRED)在windows平台怎么解
CMake中的find_package(xxx REQUIRED)在windows平台怎么解
478 0
Android Framework增加API 报错 Missing nullability on parameter
Android Framework增加API 报错 Missing nullability on parameter
423 1
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
584 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等