相关疑难解决方法(0)

dart工厂构造函数标识符的优点

我一直在调查我的Flutter应用程序的JSON解析,并且有一个关于我无法解决的工厂构造函数的问题.我试图理解使用工厂构造函数和普通构造函数的优点.例如,我看到很多JSON解析示例创建了一个带有JSON构造函数的模型类,如下所示:

class Student{
  String studentId;
  String studentName;
  int studentScores;

  Student({
    this.studentId,
    this.studentName,
    this.studentScores
  });

  factory Student.fromJson(Map<String, dynamic> parsedJson){
    return Student(
      studentId: parsedJson['id'],
      studentName : parsedJson['name'],
      studentScores : parsedJson ['score']
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我也看到过相同数量的例子,它们并没有将构造函数声明为工厂.两种类型的classname.fromJSON构造函数都是从JSON数据创建一个对象,因此将构造函数声明为工厂还是在这里使用工厂是多余的?

dart flutter

16
推荐指数
2
解决办法
8075
查看次数

标签 统计

dart ×1

flutter ×1