error • The parameter ‘name‘ can‘t have a value of ‘null‘ because of its type错误

简介: error • The parameter ‘name‘ can‘t have a value of ‘null‘ because of its type错误

问题描述

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)

2c4857bb2c4d4a7b846074e9719a4a6a.png

原因分析

报错是说参数不能为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;


相关文章
|
存储 开发者
HashMap和Hashtable的key和value可以为null吗,ConcurrentHashMap呢
HashMap的key可以为null,value也可以为null;Hashtable的key不允许为null,value也不能为null;ConcurrentHashMap的key不允许为null
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
287 0
|
Shell
Image provider: AssetImage(bundle: null, name: “assets/images/hot.png”) Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9d9f7(), name: “assets/images/hot.png”, scale: 1) 图像无法加载,并且其他图标图像也出错的解决方案-优雅草卓伊凡
Image provider: AssetImage(bundle: null, name: “assets/images/hot.png”) Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9d9f7(), name: “assets/images/hot.png”, scale: 1) 图像无法加载,并且其他图标图像也出错的解决方案-优雅草卓伊凡
255 12
|
Ubuntu Linux Windows
wsl重装Ubuntu遇到的一些问题( WslRegisterDistribution failed with error: 0x80041002 Error: 0x80041002 (null)、重置网络后WLAN图标消失)
wsl重装Ubuntu遇到的一些问题( WslRegisterDistribution failed with error: 0x80041002 Error: 0x80041002 (null)、重置网络后WLAN图标消失)
|
API 计算机视觉
Using ‘value‘ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value s
本文讨论了OpenCV中使用`createTrackbar`时遇到的"Using ‘value’ pointer is unsafe and deprecated"警告,并提供了通过设置空指针或使用回调函数来解决这个问题的方法。
Using ‘value‘ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value s
解决微软云Azure Function运行报错-Value cannot be null. (Parameter ‘provider‘)
解决微软云Azure Function运行报错-Value cannot be null. (Parameter ‘provider‘)
363 4
|
Dart 开发工具
解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null
解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null
200 1
|
C++ Python
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
214 0
|
JavaScript 前端开发 C++
【Azure Function】调试 VS Code Javascript Function本地不能运行,报错 Value cannot be null. (Parameter 'provider')问题
【Azure Function】调试 VS Code Javascript Function本地不能运行,报错 Value cannot be null. (Parameter 'provider')问题
182 0
|
SQL 关系型数据库 MySQL
实时计算 Flink版产品使用合集之从MySQL同步数据到Doris时,历史数据时间字段显示为null,而增量数据部分的时间类型字段正常显示的原因是什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。

热门文章

最新文章