参数 ‘price’ 不能具有值 ‘null’,因为其类型不允许,但隐式默认值为 ‘null’

huangapple go评论111阅读模式
英文:

The parameter 'price' can't have a value of 'null' because of its type, but the implicit default value is 'null'

问题

以下是您要翻译的内容:

> 参数 'price' 不能具有 'null' 的值,因为它的类型不允许,但隐式默认值是 'null'。
尝试添加显式的非 'null' 默认值或 'required' 修饰符。dartmissing_default_value_for_parameter
{num price}

查看图片描述

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

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

final products = [
  Item(
      id: "Codepur001",
      name: "iPhone 12 Pro",
      desc: "Apple iPhone 12 ",
      price: 999,
      color: "#33505a",
      image: "https://www.pngmart.com/files/15/Apple-iPhone-12-PNG-HD.png")
];
英文:

Note I Do not want to add required, I am looking for other solution.

>The parameter 'price' 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
{num price}

enter image description here

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

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

final products = [
  Item(
      id: "Codepur001",
      name: "iPhone 12 Pro",
      desc: "Apple iPhone 12th generation",
      price: 999,
      color: "#33505a",
      image: "https://www.pngmart.com/files/15/Apple-iPhone-12-PNG-HD.png")
];

答案1

得分: 1

因为类中的变量是非空的,您应该将它们设置为可空或者在构造函数中设置为必需,如下所示:

将它们设为必需

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

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

将它们设为可空

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

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

或者,您可以使用默认值,而不是使用 required,如下所示:

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

  Item({
    this.id = '',
    this.name = '',
    this.desc = '',
    this.color = '',
    this.image = '',
    this.price = 0,
  });
}

英文:

Because the variables in the class are non-null, you should either make them nullable or make them required compulsory from the constructor like so:

Making them required

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

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

Making them nullable

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

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

Alternatively, you can assign default values instead of using required like so:

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

  Item({
    this.id = '',
    this.name = '',
    this.desc = '',
    this.color = '',
    this.image = '',
    this.price = 0,
  });
}

huangapple
  • 本文由 发表于 2023年6月11日 23:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定