Search code examples
flutterdartdart-null-safety

The parameter 'id' can't have a value of 'null' because of its type


I am making a catalog app, I have to named the items but it's not working.

class item {
  final String id;
  final String name;
  final String desc;
  final num price;
  final String color;
  final String image;

  item({this.id, this.name, this.desc, this.price, this.color, this.image});
}

The Error: {String id} Type: String

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.

Full Code


Solution

  • Since you're using named arguments, the values can be null. You have to add the required keyword:

    class item {
      final String id;
      final String name;
      final String desc;
      final num price;
      final String color;
      final String image;
    
      item({required this.id, required this.name, required this.desc, required this.price, required this.color, required required this.image});
    }
    

    Or, make the values nullable using ?:

    class item {
      final String? id;
      final String? name;
      final String? desc;
      final num? price;
      final String? color;
      final String? image;
    
      item({this.id, this.name, this.desc, this.price, this.color, this.image});
    }