top of page

[Flutter/dart]LateInitializationError with late final variable


Phenomenon

There is a class like below.

class SomeClass{

  late final String? hoge;
  
  SomeClass({this.hoge}){
    hoge??= ""; //1
  }
}

If hoge is null (i.e. no value is set for the argument), I will get a lateInitialization error at point 1.

I thought it's okay to assign a value here, because it is "late"...



Cause

Since it is "initialized" at "this.hoge", subsequent value assignments will be lateInitialization, even if the value is null.

I thought that it was not initialized yet because it was not assigned if it was null.



Solution

class SomeClass{

  late final String? hoge;
  
  SomeClass({String? hoge}){
    this.hoge = hoge?? "";
  }
}

Recent Posts

See All

[C] Array of function pointers

What want to do As stated in the title, I would like to define an array of function pointers. Specifically, for example, the third...

Comments


category

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page