Home > Back-end >  Optional prop/property in Flutter
Optional prop/property in Flutter

Time:01-18

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
    });
}
  •  Tags:  
  • Related