Home > OS >  The parameter 'id' can't have a value of 'null' because of its type, but th
The parameter 'id' can't have a value of 'null' because of its type, but th

Time:09-29

I am following a Flutter tutorial that has such a following code, but code doesn't work on my computer, and I don't know how to fix it:

import 'package:flutter/foundation.dart';

class CartItem {
  final String id;

  CartItem({
    @required this.id,
  });
}

But I get such these errors:

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}

CodePudding user response:

You can just replace @required this.id with required this.id.

CodePudding user response:

You have a few options depending on your own project...

Option1: Make id nullable, you can keep @required or remove it.

class CartItem {
  final String? id;

  CartItem({
    this.id,
  });
}

Option2: Give a default (non-null) value to id

class CartItem {
  final String id;

  CartItem({
    this.id="",
  });
}

more in this link

  • Related