【发布时间】:2021-09-11 22:34:47
【问题描述】:
如果在 Flutter 中使用 null 安全性而不使用 required 关键字,如何在构造函数中初始化 List<Object>?
我有以下代码:
List<String> test = [];
MyConstructor({this.test});
有一个编译错误告诉我添加 required 关键字,但如果我这样做:
List<String> test = [];
MyConstructor({this.test=[]});
这给了我一个错误,the default value of a named param must be constant。
当然,我可以输入 required 关键字,一切都很好,但是在我的所有构造函数中,我需要添加一个空的 List,这很无聊,因为只有 10% 的时间我需要传递一个非空的 @987654329 @。
您能简单地确认required 关键字是唯一的选项吗?
谢谢
【问题讨论】: