0

Error1 :

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();
  }

[Error Message]

Non-nullable instance field 'isDark' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.

Error2 :

        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);
                },
              );
            }),
          ],
        )),

[Error Message]

Couldn't infer type parameter 'T'. Tried to infer 'dynamic' for 'T' which doesn't work: Parameter 'onChanged' declared as 'void Function(T?)?' but argument is 'void Function(Object?)'. The type 'dynamic' was inferred from: Parameter 'items' declared as
'List<DropdownMenuItem>?' but argument is 'List<DropdownMenuItem>'. Parameter 'value' declared as 'T?' but argument is 'int'. Consider passing explicit type argument(s) to the generic.

Hello, This is a null-safety error that occurred after the version update to 2.12.3. Most of the errors in my project have resolved. The above two types of errors cannot be resolved. Is there a workaround?

doan kim
  • 25
  • 1
  • 8

1 Answers1

1

You need to either mark your lists inside the model as nullable, or make the optional parameter required.

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

  ...

Or you can mark these fields in constructor as 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,
  });
}

But since this looks like a api response model, you should mark everything as nullable and check for errors accordingly.

Pionix
  • 413
  • 2
  • 8