【问题标题】:Flutter Non-nullable instance Field errorFlutter 不可为空的实例字段错误
【发布时间】:2021-08-02 14:05:18
【问题描述】:

错误1:

class _ProductDataSource extends DataGridSource {
  _ProductDataSource(List<_Product> products) {
    _products = products.map<DataGridRow>((e) {
      return DataGridRow(cells: [
        DataGridCell<String>(
          columnName: 'name',
          value: e.name,
        ),
        DataGridCell<double>(
          columnName: 'price',
          value: e.price,
        ),
        DataGridCell<int>(
          columnName: 'quantity',
          value: e.quantity,
        ),
        DataGridCell<double>(
          columnName: 'total',
          value: e.total,
        ),
      ]);
    }).toList();
  }

[错误信息]

必须初始化不可为空的实例字段“isDark”。尝试添加 初始化表达式,或在此添加字段初始化 构造函数,或将其标记为“迟到”。

错误2:

        Expanded(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text("type1"),
            Obx(() {
              return DropdownButton( // Error!
                isExpanded: true,
                hint: paramController.hwType.value == null
                    ? Text("type1")
                    : Text(
                        convertToTypeName.hwType(hwType
                            .values[paramController.hwType.value]
                            .toString()),
                        style: TextStyle(color: Colors.blue),
                      ),
                items: hwTypeItems(),
                value: paramController.hwType.value,
                onChanged: (val) {
                  paramController.setHwType(val as int);
                },
              );
            }),
          ],
        )),

[错误信息]

无法推断类型参数“T”。试图推断“T”的“动态” 这不起作用:参数'onChanged'声明为'void 函数(T?)? 但参数是'void Function(Object?)'。 'dynamic' 类型推断自:参数 'items' 声明为
'列表?' 但参数是'List'。参数“值”声明为“T?” 但论点是'int'。考虑将显式类型参数传递给泛型。

你好, 这是版本更新到 2.12.3 后发生的空安全错误。 我项目中的大部分错误都已解决。 以上两种错误都无法解决。 有解决办法吗?

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies flutter-web


    【解决方案1】:

    您需要将模型内的列表标记为nullable,或者设置可选参数required。

    class AsDetailModel {
      bool? success;
      String? code;
      String? msg;
      int? rowSize;
      List<String>? fieldType; // here
      List<String>? fieldName; //
      List<AsDetail>? list;    //
    
      ...
    

    或者您可以在构造函数中将这些字段标记为required。

    class AsDetailModel {
      bool? success;
      String? code;
      String? msg;
      int? rowSize;
      List<String> fieldType;
      List<String> fieldName;
      List<AsDetail> list;
    
      AsDetailModel({
        this.success,
        this.code,
        this.msg,
        this.rowSize,
        required this.fieldType,
        required this.fieldName,
        required this.list,
      });
    }
    

    但是由于这看起来像一个 api 响应模型,您应该将所有内容标记为可为空并相应地检查错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2021-08-10
      • 2021-09-29
      • 2022-07-19
      • 1970-01-01
      • 2022-08-18
      • 2023-01-07
      相关资源
      最近更新 更多