Search code examples
flutterdartdart-null-safety

Flutter error while using late initializer


Im trying to connect my flutter web with discord oauth2 but i've got problem with late initializer.

Flutter suggests me to use "late" in:

Computed<ThemeData> _$currentThemeDataComputed;

After i add "late" then when im trying to run my website i've got this error:

LateInitializationError: Field '_$currentThemeDataComputed' has not been initialized.

Is there another way to fix it?


Solution

  • I will prefer making data nullable. like DataType? nullableVariableName.

    Computed<ThemeData>? _$currentThemeDataComputed;
    

    Now you can check if the value is null or not while read it.

    Dont use ! directly without check null. Better way of handling will be

    if(nullableVariableName!=null){
      /// use this variable 
    }
    

    More about null-safety