In kotlin we can check if the 'late' type variables are initialized like below
lateinit var file: File if (this::file.isInitialized) { ... }
Is it possible to do something similar to this in Dart..?
AVOID late variables if you need to check whether they are initialized. Dart offers no way to tell if a late variable has been initialized or assigned to. If you access it, it either immediately runs the initializer (if it has one) or throws an exception.
How to Solve Error? Remember: You have to assign something to the late variable before using it. Here, we have created a nullable variable, which is assigned to the Text() widget, When you use Text(name!) null check operator instead shown like above, you may get "Null check operator used on a null value" Error.
In Dart, we use the late keyword to declare variables that will be initialized later. These are called non-nullable variables as they are initialized after the declaration. Hence, we use the late keyword. Note: Once we declare a non-nullable late variable, the variable can't be null at runtime.
Unfortunately this is not possible.
From the docs:
AVOID late variables if you need to check whether they are initialized.
Dart offers no way to tell if a late variable has been initialized or assigned to. If you access it, it either immediately runs the initializer (if it has one) or throws an exception. Sometimes you have some state that’s lazily initialized where late might be a good fit, but you also need to be able to tell if the initialization has happened yet.
Although you could detect initialization by storing the state in a late variable and having a separate boolean field that tracks whether the variable has been set, that’s redundant because Dart internally maintains the initialized status of the late variable. Instead, it’s usually clearer to make the variable non-late and nullable. Then you can see if the variable has been initialized by checking for null.
Of course, if null is a valid initialized value for the variable, then it probably does make sense to have a separate boolean field.
https://dart.dev/guides/language/effective-dart/usage#avoid-late-variables-if-you-need-to-check-whether-they-are-initialized
Some tips I came up with from advice of different dart maintainers, and my self-analysis:
late
usage tips:late
modifier on variables if you are going to check them for initialization later.late
modifier for public-facing variables, only for private variables (prefixed with _
). Responsibility of initialization should not be delegated to API users. EDIT: as Irhn mentioned, this rule makes sense for late final
variables only with no initializer expression, they should not be public. Otherwise there are valid use cases for exposing late
variables. Please see his descriptive comment!late
variables in all constructors, exiting and emerging ones.late
variable inside unreachable code scenarios. Examples: late
variable initialized in if
clause but there's no initialization in else
, and vice-versa.late
variable is initialized.Please point out any errors/additions to this.
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With