我正在关注一个 Flutter 教程,其中包含以下代码,但代码在我的计算机上不起作用,我不知道如何修复它:
import 'package:flutter/foundation.dart';
class CartItem {
final String id;
CartItem({
@required this.id,
});
}
但是我得到了这样的错误:
The parameter 'id' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
{String id}
最佳答案
最新的 dart 版本现在支持声音 null safety .教程必须使用旧版本。
要表明一个变量可能具有 null 值,只需添加 ?对其类型声明:
class CartItem {
final String? id = null;
CartItem({
this.id,
});
}
或
class CartItem {
final String? id;
CartItem({
this.id,
});
}
关于flutter - 参数 'id' 因其类型不能有 'null' 的值,但隐含的默认值为 'null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69369772/