Lint don't override field in flutter

1,053

The fields id and uid already are defined within the UserEntity class so it is redundant to declare them again as fields in the UserModel class.

class UserModel extends UserEntity {
  const UserModel({required int id, String? uid}) : super(id: id, uid: uid);
}

EDIT: as of dart 2.17 you can simplify the above code by using super initializers.

class UserModel extends UserEntity {
  const UserModel({required super.id, super.uid});
}
Share:
1,053
unice
Author by

unice

Updated on January 02, 2023

Comments

  • unice
    unice over 1 year

    I'm new in using Lint and I'm getting a blue underline on my UserModel variables with a message of Don't override fields and Annotate overridden members. I'm having a hard time understanding the Good and Bad rules in the example docs. I still get the same message after adding @override.

    class UserModel extends UserEntity {
      final int id;
      final String? uid;
     
      const UserModel(
          {required this.id,
          this.uid})
          : super(
              id: id,
              uid: uid,
            );
    }
    
    
    //
    
    class UserEntity extends Equatable {
      const UserEntity({
        required this.id,
        this.uid,
        this.provider,
       });
    
      final int id;
      final String? uid;
     
      static const empty = UserEntity(id: 0, uid: '');
    
      @override
      List<Object> get props => [id];
    }
    
    • jamesdlin
      jamesdlin over 2 years
      The overridden_fields lint is warning you that you're overriding a field. Adding an @override attribute won't help. If you don't want the warning, don't override the base fields, use a getter and/or setter in the derived class instead if possible, or use // ignore: overridden_fields to suppress the lint. Also see github.com/dart-lang/linter/issues/2428.