Why does Flutter only give me a warning instead of an error during a missing required function argument?
Let's take this code for example:
FlatButton(
color: Colors.red,
onPressed: () {
doSomething();
},
),
The FlatButton requires a @required Widget child
and @required VoidCallback onPressed
argument. The child is missing here, but it still compiles and only gives me a warning.
Are @Required
arguments not that important to give an error? Can somebody explain me why this only gives a warning instead of an error?
@required
, as designated by the @
, is just an annotation (provided from package:meta
) and is not a language feature. It tells tools such as the Dart analyzer to generate a warning, but neglecting to supply the missing argument is still technically legal, so it will compile.
You can make the Dart analyzer treat it as an error instead of as a warning, but again, that won't help if you neglect to run the analyzer.
Note that the null-safe version of Dart adds a new required
keyword that replaces the @required
annotation, and that therefore will be a proper language feature and will generate compilation errors if the argument is omitted.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments