前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【错误记录】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 )

作者头像
韩曙亮
发布2023-03-29 15:46:02
4200
发布2023-03-29 15:46:02
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、报错信息


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

代码语言:javascript
复制
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 类似 , 修改后 , 编译不再报错 ;

代码语言:javascript
复制
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});
}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、报错信息
  • 二、解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档