Issue
In Visual Studio Code, no errors seem to arise when an @required parameter is omitted when invoking a Dart function. Do I have to do something to get the analyzer working? Or are errors being flagged and I’m just not seeing them? Any help would be appreciated …
import 'package:meta/meta.dart';
void sayHello({@required String to, bool inEnglish}){
if(inEnglish == null || inEnglish){
print("Hello, $to");
} else {
print("Bonjour, $to");
}
}
main(){
sayHello(inEnglish: true); // output: Hello, null, no complaints about **to** missing
}
Solution
The Dart language has required positional parameters, optional positional parameters, and optional named parameters. Sadly, Dart has no support for required named parameters.
The @required
annotation doesn’t actually do anything. It’s just a workaround added by Flutter that the analyzer can use to add a warning when you don’t pass a parameter that is marked with it. But it won’t prevent you from omitting the “required” parameter, and code that does so will still compile and run perfectly well.
Answered By – Abion47
Answer Checked By – Jay B. (FlutterFixes Admin)