Home > Blockchain >  Flutter lint - Don't override fields
Flutter lint - Don't override fields

Time:12-08

I am trying to resolve the lint rule warning, Don't override fields and I cannot seem to crack it. May I please get some assistance on how to go about it

My code:

 @override
  final Key? key; // LINT warning is caused by key

  FeedViewArticle(
    this.articleId,
    this.image, {
   
    this.key,
    
  }) : super(key: key);

I tried removing the @override and it still does not work, your help will be appreciated.

CodePudding user response:

just remove the field entirely. It inherited it from the super class so it already has it. No need to define it again. You can write the constructor as:

  FeedViewArticle(
    this.articleId,
    this.image, {
    super.key,
  });

CodePudding user response:

The lint warning "Don't override fields" is typically caused by a field in a class that has the same name as a field in the superclass, and that is marked with the @override annotation. In this case, the warning is being triggered by the key field in your FeedViewArticle class, which has the same name as a field in the StatelessWidget superclass and is marked with the @override annotation.

To fix this warning, you will need to remove the @override annotation from the key field in your FeedViewArticle class. The @override annotation is used to indicate that a method or field in a subclass is intended to override a method or field with the same name in the superclass. However, in this case, the key field in the StatelessWidget superclass is not marked with the @override annotation, so it cannot be overridden.

Here is an example of how you can modify your FeedViewArticle class to remove the @override annotation and fix the lint warning:

class FeedViewArticle extends StatelessWidget {
 final int articleId;
 final Image image;

 // Remove the @override annotation from the key field
 final Key? key;

 FeedViewArticle(
this.articleId,
this.image, {

this.key,

  }) : super(key: key);

 // Other methods and code for the class
}

After you remove the @override annotation from the key field, the lint warning should no longer be generated. You can then run the flutter analyze command to verify that the warning has been fixed.

  • Related