构造函数在Dart类中。

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

Constructor in dart classes

问题

The difference between using curly brackets inside a constructor in Dart classes and not using them is that when you use curly brackets, you're creating named parameters in the constructor. Named parameters are optional and can be null, which is why you receive no errors when you don't provide values for them.

In your first example with curly brackets:

class Animal{
  String name;
  int age;

  Animal({this.name, this.age});
}

You define named parameters, so it's acceptable to not provide values for name and age, making them nullable.

In your second example without curly brackets:

class Animal{
  String name;
  int age;

  Animal(this.name, this.age);
}

You define positional parameters, which must be provided when creating an instance of the class. This is why you receive errors when you don't provide values for name and age.

英文:

What is difference when you use curly brackets inside constructor in dart classes and when you don't use it. And why you can not have not nullable and not initialized fields inside constructor when you use curly brackets, but you can when you don't use curly brackets.

Here is example:

class Animal{
  String name;
  int age;

  Animal({this.name, this.age});
}

When I write class like this, there is error which says: "The parameter 'name' can't have a value of 'null' because of its type, but the implicit default value is 'null'.", and same for 'age'.

class Animal{
  String name;
  int age;

  Animal(this.name, this.age);
}

But when I don't use curly brackets there is no error, it allows to have null fields in constructor.

答案1

得分: 1

使用大括号时,您定义了命名参数。例如:

class Animal {
  String name;
  int age;

  Animal({this.name, this.age});
}

您应该像这样实例化:

Animal(
  name: "Dog",
  age: 5,
);

如果您不使用大括号定义构造函数,则定义了位置参数。

class Animal {
  String name;
  int age;

  Animal(this.name, this.age);
}

然后,您应该像这样实例化:

Animal("Cat", 3);

如果您添加required关键字,您将使参数变为强制性的。

如果您用?在类型后面声明一个可为空的属性,例如:

int? age

那么您的name参数将变为非强制性的。

英文:

When you use braces, you define named parameters. For example:

class Animal{
  String name;
  int age;

  Animal({this.name, this.age});
}

You should instanciate like this:

Animal(
  name: "Dog",
  age: 5,
);

If you define your constructor without braces, you defined positionnal parameters.

class Animal{
  String name;
  int age;

  Animal(this.name, this.age);
}

Then you should instantiate like this:

Animal("Cat", 3);

If you add required keyword, you make the parameter mandatory.

If you declare a property nullable with a ? after type, for ewample

int? age

your name parameter become not mandatory.

答案2

得分: 0

以下是代码部分的翻译:

with braces

void main() {
  print(Animal(name: "Stark")); // 因为它是可空的,所以我不必输入年龄
  // 这等同于
  print(Animal(name: "Stark", age: null)); // 使用大括号后,输入不会被映射
  print(Animal(age: null)); // 错误,需要提供name
}

class Animal {
  String name; // 不可空
  int? age; // 可空

  Animal({required this.name, this.age});
}

注意:代码部分已翻译,不包括翻译请求中的其他信息。

英文:

with braces

 void main() {
  print(Animal(name: "Stark")) ; // I dont have to enter age because 
  //  its nullable
  // it is the same as 
   print(Animal(name: "Stark" , age: null))  ; // after using braces inputs not are mapped
    print(Animal(age: null)) ; // error   name: is required
  
}

  class Animal{
  String name; // not nullable
  int? age;  //  nulabble

  Animal({required this.name, this.age}); // 
} 

so you cant map not nullable value to null because Class Null is not a subtype of String or int or any other class after null safety

huangapple
  • 本文由 发表于 2023年5月7日 19:38:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193685.html
匿名

发表评论

匿名网友

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

确定