List can't access an object before it is initialized in dart

Issue

I created a house object to have member in a list called ‘favHouses’. But dart doesn’t let me to add those members before i initialize in constructor. Here is my code and the error that i get.

List<House> favHouses = [houseAlanya];
House houseAlanya = new House(..); 

the error i get:

The instance member ‘houseAlanya’ can’t be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
dart(implicit_this_reference_in_initializer)

Solution

With Dart 2.12, you can use the late keyword for lazy initialization.

late List<House> favHouses = [houseAlanya];

Answered By – CopsOnRoad

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.