why we use these keyword? when we need to use it in dart programming language? Please explain with briefly.
CodePudding user response:
Each keyword could have its own answer but a quick overview would be the following:
final
is used when we want to define a variable we want to only be given a value once when initializing the value which can then later never be changed.const
is used for compile-time constant values and is compiled with a value when compiling the program. The value cannot be changed at runtime. Dart also gives us a guarantee that if we call the sameconst
constructor with the same input parameters, we will get the same instance in memory.
For final
and const
you can read more about them in the Dart Language Tour: https://dart.dev/guides/language/language-tour#final-and-const
late
is part of Dart null-safety feature and is used to tell the compiler that you promise this variable will be given a value before first usage of the variable. This is used because Dart would otherwise complain if you are defining a non-nullable variable without any value when initializing it.
You can read more about late
in the Dart Language Tour: https://dart.dev/guides/language/language-tour#late-variables
- (
@required
is no longer part of Dart but is replaced withrequired
) is used to indicate that a named parameter for a method MUST be given a value. Named parameters are otherwise optional which is a problem when we are using null-safety.
You can read more about the need of the required
keyword in the Dart Language Tour: https://dart.dev/guides/language/language-tour#named-parameters
And the following which describes why we need this keyword when using null-safety: https://dart.dev/null-safety/understanding-null-safety#required-named-parameters