How to Use @required Annotation On Flutter Constructor Parameters??

· 3 min read
How to Use @required Annotation On Flutter Constructor Parameters
How to Use @required Annotation On Flutter Constructor Parameters

Earlier we have been through various articles like How to Pass Data to a Stateful Widget in Flutter. So in this article, we will go through how to use @required Annotation On Flutter Constructor Parameters.

Are you ready for the same? let’s dive into the same.

How to Use @required Annotation On Flutter Constructor Parameters??

In order to solve we need to use Annotations that need to be imported. Adding at the top of your file.

import 'package:flutter/foundation.dart';

should fix it.

Annotations the Dart Analyzer understands are provided by the meta-package.

  • To make it easier for Flutter Developers, the Flutter team decided to add the meta package to the Flutter SDK and re-export it in flutter/foundation.dart. The annotations by flutter are therefore exactly the same as those provided by the meta-package and you can as well add meta to your dependencies in pubspec.yaml and import annotations from there if you prefer. If you want to reuse code between for example Angular Dart and Flutter that is the preferred way because code that imports from a package: flutter/… can’t be used in Dart web applications.
  • Please import package “meta” at the beginning of the source file.
    // @required is defined in the meta.dart package
    import 'package:meta/meta.dart';

    So if the error persists, try the following on the CLI:

    pub upgrade

I found that this problem can happen if your class has a variable called required.

class TextFieldInputWidget extends StatefulWidget {
  final String title;
  final bool required;

  const TextFieldInputWidget({@required this.title, this.required = false});

  @override
  _TextFieldInputWidget createState() => _TextFieldInputWidget();
}

So now, Just change the variable name to something else, like “require

Conclusion:

So in this article, We have been through How to Use @required Annotation On Flutter Constructor Parameters.

Keep Learning !!! Keep Fluttering !!! Stay Connected !!!

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply