I'm in Flutter, and I know something about React Native. In React Native I create optional props like this:
foo?: boolean;
How do I do the same thing in Flutter?
I looked here and didn't find it.
CodePudding user response:
The same way to you reproduce optional props in Flutter is something like this:
class MyClass {
final String myProperty;
final List<int> anotherProp;
final String? nullSafetyProp; //can be optional
late final String lateProp; // shold be initialized in Future. Before read the value
MyClass({
required this.myProperty,
required this.anotherProp,
this.nullSafetyProp, // optional property
required this.lateProp
});
}
- about keyword
final: https://dart.dev/guides/language/language-tour#final-and- - about
nullsafety: https://dart.dev/null-safety latevariables: https://dart.dev/null-safety/understanding-null-safety#late-variableslate finalvariables: https://dart.dev/null-safety/understanding-null-safety#late-final-variables
