Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check 'late' variable is initialized in Dart

Tags:

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..?

like image 283
towhid Avatar asked Apr 21 '21 14:04

towhid


People also ask

How do you know if a late variable is initialized 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 do you solve late initialization in flutter?

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.

What is late variable in Dart?

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.


2 Answers

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

like image 108
M123 Avatar answered Sep 22 '22 15:09

M123


Some tips I came up with from advice of different dart maintainers, and my self-analysis:

late usage tips:

  • Do not use late modifier on variables if you are going to check them for initialization later.
  • Do not use 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!
  • Do make sure to initialize late variables in all constructors, exiting and emerging ones.
  • Do be cautious when initializing a late variable inside unreachable code scenarios. Examples:
    • late variable initialized in if clause but there's no initialization in else, and vice-versa.
    • Some control-flow short-circuit/early-exit preventing execution to reach the line where late variable is initialized.

Please point out any errors/additions to this.

Enjoy!

Sources:

  • eernstg's take
  • Hixie's take
  • lrhn's take
  • leafpetersen's final verdict as of 2021 10 22
  • Effective Dart
  • Self-analysis on how to approach this with some common-sense.
like image 30
om-ha Avatar answered Sep 25 '22 15:09

om-ha