How to use @required annotation on Flutter constructor parameters?
Annotations need to be imported
Adding at the top of your file
import 'package:flutter/foundation.dart';
should fix it.
Annotations the DartAnalyzer 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 these 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 AngularDart and Flutter that is the preferred way because code that imports from package:flutter/...
can't be used in Dart web applications.
Does your code include the following code?
import 'package:meta/meta.dart';
If your code contains the code above and you get errors, check pubspec.yaml
file:
dependencies:
meta: ^1.4.0
flutter:
sdk: flutter
Pay attention to the meta section from the above sample.
If the error persists, try the following on the CLI:
pub upgrade
Please import package "meta" at the beginning of the source file.
// @required is defined in the meta.dart package
import 'package:meta/meta.dart';