无法在工厂的初始化程序中访问 dart 实例成员

dart instance member can't be accessed in an initializer from a factory

我正在尝试清除我的 lint 规则,但很难找到 'implicit_this_reference_in_initializer'

的解决方案
class MyModel extends MyEntity {

 MyModel({ required this.myId, required this.myName } ) 
  : super(myId: tmyId, myName: tmyName);

 int tmyId;
 String tmyName;

 factory MyModel.fromJson(Map<String, dynamic> json) => MyModel(
   myId : (json['my_id'] as num).toInt(),
   myName : (json['my_name'] as String).toString() );

以上代码在初始化错误中产生了隐含的 this 引用。

如果我切换到使用相同的变量名:myId 和 myName,我会得到 lint 规则:'overrides-fields':不要覆盖字段:int myId;和字符串我的名字;变量。

在这一点上,我选择只接受 L(int..hah!明白了吗?L ..lint...) 错误“不要覆盖字段”,因为这似乎不太重要两害相权。

感谢指点。

你的问题不是很清楚,但是定义相同的参数是没有限制的

void main() async {
  MyModel m = MyModel.fromJson({'my_id':0 , 'my_name': 'test'});
  print(m.myName);
  print(m.myId);
}

class MyEntity {
  final int myId;
  final String myName;
  MyEntity({@required this.myId, @required this.myName});
}

class MyModel extends MyEntity {
  final int myId;
  final String myName;
  MyModel({@required this.myId,@required this.myName}) : super(myId: myId, myName: myName);
  
  factory MyModel.fromJson(Map<String, dynamic> json){
    return MyModel(myId: json['my_id'], myName: json['my_name']);
  }
}

有很多变量命名混乱,
所以我只会添加一个关于实际情况的示例。

示例:

class MyModel extends MyEntity {

 final int myId;
 final String myName;
  
 MyModel({this.myId, this.myName}) 
  : super(tmyId: myId, tmyName: myName);

 factory MyModel.fromJson(Map<String, dynamic> json) => MyModel(
   myId : (json['my_id'] as num).toInt(),
   myName : (json['my_name'] as String).toString() );
  
}

class MyEntity {
  final String tmyName;
  final int tmyId;
  
  const MyEntity({this.tmyName, this.tmyId});
}

问题出在您的默认构造函数上,而不是您的 factory 构造函数:

class MyModel extends MyEntity {

 MyModel({ required this.myId, required this.myName } ) 
  : super(myId: tmyId, myName: tmyName);

 int tmyId;
 String tmyName;

required this.myIdrequired this.myName是错误的; MyModel 本身没有具有这些名称的成员。构造函数然后尝试使用 tmyIdtmyName 调用基础 class 构造函数,但是 tmyIdtmyName 成员尚未初始化,并且 无论如何。

正确的解决方法是删除 tmyIdtmyName。如果基础 class 已经提供了相应的成员,那么 tmyIdtmyName 就没有用了:

 MyModel({required int myId, required String myName}) 
  : super(myId: myId, myName: myName);

如果您确实需要 tmyIdtmyName 成员,那么:

 MyModel({required int myId, required String myName}) 
  : tmyId = myId,
    tmyName = myName,
    super(myId: myId, myName: myName);

感谢所有反馈。这是最终产品:向@jamesdin 大声喊叫,让我知道我在那里有多余的绒毛,还有来自 AdvancedJson2Dart IntelliJ 插件的 Mohammed Binmiskeen 帮助我将缺失的部分放在一起。

///json model implementation used by either local or remote storage
class BookOfTocModel extends BookOfTocEntity {
  ///constructor model forms data sources
  const BookOfTocModel({
    required int bookId,
    required String bookName,
    required String bookDescription,
    required String bookLanguage,
    required List<ChapterDataListEntity> chapterDataList,
  }) : super(
          bookDescription: bookDescription,
          bookId: bookId,
          bookName: bookName,
          bookLanguage: bookLanguage,
          chapterDataList: chapterDataList,
        );

  ///fromJson forms the model based off BookOfTocEntity
  factory BookOfTocModel.fromJson(Map<String, dynamic> json) => BookOfTocModel(
        bookId: json['book_id'] as int,
        bookName: json['book_name'] as String,
        bookDescription: json['book_description'] as String,
        bookLanguage: json['book_language'] as String,
        chapterDataList: (json['chapters'] as List<dynamic>)
            .map(
                (e) => ChapterDataListModel.fromJson(e as Map<String, dynamic>))
            .toList(),
      );

  ///Reforms BookOfTocModel data to json format for placing in local cache
  Map<String, dynamic> toJson(BookOfTocModel instance) => <String, dynamic>{
        'book_id': instance.bookId,
        'book_name': instance.bookName,
        'book_description': instance.bookDescription,
        'book_language': instance.bookLanguage,
        'chapter_list': instance.chapterDataList,
      };
}

如果有人看到任何错误,请告诉我。但这没有产生 lint 问题。