【问题标题】:Instance members can't be accessed from a factory constructor. Flutter error无法从工厂构造函数访问实例成员。颤振错误
【发布时间】:2022-01-03 11:33:43
【问题描述】:

我收到如下错误:

不能从工厂构造函数访问实例成员。 尝试删除对实例成员的引用。

有什么解决办法吗?

class DepartureModel {
  String route;
  String departureTime;
  String arrivalTime;
  String tourType;
  List<String> daysOfWeek;

  DepartureModel({
    required this.route,
    required this.departureTime,
    required this.arrivalTime,
    required this.tourType,
    required this.daysOfWeek,
  });

  //method that assign values to respective datatype vairables

  factory DepartureModel.fromJson(Map<String, dynamic> json) {
    return DepartureModel(
      route: json['route'],
      departureTime: json['departureTime'],
      arrivalTime: json['arrivalTime'],
      tourType: json['tourType'],
      daysOfWeek: json["daysOfWeek"].forEach(
        (day) {
          daysOfWeek.add(day);
        },
      ),
    );
  }

【问题讨论】:

    标签: flutter http dart


    【解决方案1】:

    您需要对代码进行一些更改才能使其正常工作。在model 下方使用或在model 中的daysofweek 行中进行更改,如下所示:

    import 'dart:convert';
    
    DepartureModel departureModelFromJson(String str) => DepartureModel.fromJson(json.decode(str));
    
    String departureModelToJson(DepartureModel data) => json.encode(data.toJson());
    
    class DepartureModel {
        DepartureModel({
            this.route,
            this.departureTime,
            this.arrivalTime,
            this.tourType,
            this.daysOfWeek,
        });
    
        String route;
        String departureTime;
        String arrivalTime;
        String tourType;
        List<String> daysOfWeek;
    
        factory DepartureModel.fromJson(Map<String, dynamic> json) => DepartureModel(
            route: json["route"],
            departureTime: json["departureTime"],
            arrivalTime: json["arrivalTime"],
            tourType: json["tourType"],
            daysOfWeek: List<String>.from(json["daysOfWeek"].map((x) => x)),
        );
    
        Map<String, dynamic> toJson() => {
            "route": route,
            "departureTime": departureTime,
            "arrivalTime": arrivalTime,
            "tourType": tourType,
            "daysOfWeek": List<dynamic>.from(daysOfWeek.map((x) => x)),
        };
    }
    

    【讨论】:

      【解决方案2】:

      您在分配 daysOfWeek 时尝试访问它,这就是编译器抱怨的原因,因为它还不知道 daysOfWeek 的值。

      真正有效的解决方法是在工厂构造函数中创建一个新列表,然后在完成循环后将其分配给daysOfWeek,如下所示:

      factory DepartureModel.fromJson(Map<String, dynamic> json) {
          final tempDaysOfWeek = [];
          json["daysOfWeek"].forEach((day) => tempDaysOfWeek.add(day));
          return DepartureModel(
            route: json['route'],
            departureTime: json['departureTime'],
            arrivalTime: json['arrivalTime'],
            tourType: json['tourType'],
            daysOfWeek: tempDaysOfWeek,
          );
      

      您也可以将tempDaysOfWeek 命名为daysOfWeek,因为作用域将负责调用哪个变量,但这可以减轻混淆。

      没有forEach 的更简洁的用法如下:

      factory DepartureModel.fromJson(Map<String, dynamic> json) {
          return DepartureModel(
            route: json['route'],
            departureTime: json['departureTime'],
            arrivalTime: json['arrivalTime'],
            tourType: json['tourType'],
            daysOfWeek: (json["daysOfWeek"] as List).cast<String>(),
          );
      

      【讨论】:

      • 非常感谢!
      猜你喜欢
      • 2022-09-23
      • 2017-11-16
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多