首页 > 其他分享 >error • The parameter 'name' can't have a value of 'null' because of its ty

error • The parameter 'name' can't have a value of 'null' because of its ty

时间:2022-10-15 14:31:08浏览次数:52  
标签:because String ty parameter value null type final name

问题描述

error • The parameter 'name' can't have a value of 'null' because of its type, but the implicit default value is 'null' at lib\models\chat_model.dart:7:19 • (missing_default_value_for_parameter)
error • The parameter 'message' can't have a value of 'null' because of its type, but the implicit default value is 'null' at lib\models\chat_model.dart:7:30 • (missing_default_value_for_parameter)
error • The parameter 'time' can't have a value of 'null' because of its type, but the implicit default value is 'null' at lib\models\chat_model.dart:7:44 • (missing_default_value_for_parameter)
error • The parameter 'avatarUrl' can't have a value of 'null' because of its type, but the implicit default value is 'null' at lib\models\chat_model.dart:7:55 • (missing_default_value_for_parameter)

如图: 在这里插入图片描述

报错的代码:

class ChatModel {
  final String name;
  final String message;
  final String time;
  final String avatarUrl;

  ChatModel({this.name, this.message, this.time, this.avatarUrl});
}

报错是说参数不能为null,但是可能存在隐形的赋值为null的可能。代码修改成如下

class ChatModel {
  final String? name;
  final String? message;
  final String? time;
  final String? avatarUrl;

  ChatModel({this.name, this.message, this.time, this.avatarUrl});
}

改成这样后会报其的错误:

error • The argument type 'String?' can't be assigned to the parameter type 'String' at lib\pages\chat_screen.dart:25:53 • (argument_type_not_assignable)
error • The argument type 'String?' can't be assigned to the parameter type 'String' at lib\pages\chat_screen.dart:31:23 • (argument_type_not_assignable)
error • The argument type 'String?' can't be assigned to the parameter type 'String' at lib\pages\chat_screen.dart:35:23 • (argument_type_not_assignable)
error • The argument type 'String?' can't be assigned to the parameter type 'String' at lib\pages\chat_screen.dart:43:21 • (argument_type_not_assignable)

在这里插入图片描述

原因分析

报错是说参数不能为null,但是可能存在隐形的赋值为null的可能。这些参数都是在构造函数中调用。说明这些参数是必须的就可以了。

解决措施

构造函数添加required。代码如下:

class ChatModel {
  final String name;
  final String message;
  final String time;
  final String avatarUrl;

  ChatModel({required this.name, required this.message,required  this.time, required this.avatarUrl});
}

另外最新的Dart 强制null safety。定义一个变量时暂时没有赋值,记得加上late告知Dart 这个变量稍后会赋值。不然会报“Non-nullable instance field 'controller' must be initialized” 错误。 eg

late CameraController controller;

标签:because,String,ty,parameter,value,null,type,final,name
From: https://blog.51cto.com/u_15777557/5759170

相关文章

  • 【C语言_24】typedef关键词
    1.typedef定义类型别名定义int的别名int32_t:typedefintint32_t;2.typedef的使用#include<stdio.h>intmain(){typedefintint32_t;//作用域:{}里申请的,只能在{}里用i......
  • Netty源码解读 2022.10.15
    netty:4.1.9.Final核心概念:-Channel通道,贯穿一切。-ChannelHandler处理通道事件或响应。-ChannelHandlerInitializer用来初始化一个ChannelHandler。-ChannelHandl......
  • TypeScript中的元组 Tuple
    元组类型//元组类型:表示一个已知元素数量和类型的数组,各元素的类型不必相同letundata:[string,'男'|'女'];//已知数量是两个。类型分别是字符串和男或者女undata......
  • pytho异常:DataError: No numeric types to aggregate
    1.异常介绍进行分组聚合求均值(mean)的的时候出现了以下异常:但是求和(sum)却不会抛出异常。2.异常原因在进行数据处理的时候存在缺失值,而且被处理的列不是float同一类型,注......
  • PriorityQueue源码解析(一)
    ......
  • TypeScript Utility Types All In One
    TypeScriptUtilityTypesAllInOneAwaited<Type>Partial<Type>Required<Type>Readonly<Type>Record<Keys,Type>Pick<Type,Keys>Omit<Type,Keys>Exclude<Unio......
  • Entity Framework教程-数据库上下文对象(Data Context)
    更新记录转载请注明出处:2022年10月15日发布。2022年10月10日从笔记迁移到博客。DbContext对象介绍(数据库上下文对象)数据库上下文对象说明ADbContextinstancer......
  • Unity彻底删除
    步骤一正常情况的卸载,可以通过电脑管家卸载。步骤二清除unity的注册表。首先在运行中输入“regedit”打开注册表界面;删除HKEY_CURRENT_USER\Software\Unity下所有......
  • 2022-08-05-欢迎使用_Typecho
    layout:postcid:1title:欢迎使用Typechoslug:startdate:2022/08/0514:21:51updated:2022/08/0514:21:51status:publishauthor:admincategories:默认......
  • 2022-09-11-Typecho_RSS优化显示全文
    layout:postcid:26title:TypechoRSS优化显示全文slug:26date:2022/09/1115:53:38updated:2022/09/1115:53:38status:waitingauthor:admincategories:......