-1

I'm trying to get my lint rules cleared and having a hard time finding a solution for '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() );

The above code produces the implicient this reference in initializer error.

If I switch to using the same variable names: myId and myName I get the lint rule: 'overrides-fields' : Don't override fields for: int myId; and String myName; variables.

At this point, I'm opting to just take the L(int..hah! get it? L ..lint...) for the error 'Don't override fields' as that seems the lesser of two evils.

Pointers appreciated.

Gumdum
  • 157
  • 2
  • 10

4 Answers4

1

There's a lot of variable naming confusion,
so I'll just add a sample about how it should actually be.

Example:

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});
}

Darshan
  • 4,020
  • 2
  • 18
  • 49
1

The problem is with your default constructor, not with your factory constructor:

class MyModel extends MyEntity {

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

 int tmyId;
 String tmyName;

required this.myId and required this.myName are wrong; MyModel itself does not have members with those names. The constructor then attempts to call the base class constructor using tmyId and tmyName, but the tmyId and tmyName members haven't been initialized yet, and an initializer list can't read from member variables anyway.

The correct fix is to get rid of tmyId and tmyName. If the base class already provides corresponding members, then tmyId and tmyName serve no purpose:

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

If you somehow do need tmyId and tmyName members, then:

 MyModel({required int myId, required String myName}) 
  : tmyId = myId,
    tmyName = myName,
    super(myId: myId, myName: myName);
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
0

Your question is not very clear, but there is no limit to define the same parameters.

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']);
  }
}
Reza M
  • 544
  • 3
  • 10
  • Moving the declarations to the top still produces the lint rule errors. I am going to go with 'Don't override fields' and just over ride the lint error – Gumdum Jun 03 '21 at 14:44
0

Thanks for all the feedback. This is the final product: Shout out to @jamesdin for letting me know I had extra fluff in there and Mohammed Binmiskeen from AdvancedJson2Dart IntelliJ plugin that helped me put the missing pieces together.

///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,
      };
}

If anyone sees any errors, let me know. But this produced no lint issues.

Gumdum
  • 157
  • 2
  • 10