How to solve Non-nullable instance field 'catalog' must be initialized. Try adding an initializer expression,?
2,324
You need to initialize the properties CatelogModel catalog
and CartModel cart
or mark them as late. Right now its complaining that you have stated they should not be null but you've not initialize them in the constructor.
So you can either mark them as late
class MyStore extends VxStore {
late CatelogModel catalog;
late CartModel cart;
MyStore() { #Getting error here#
catalog = CatelogModel();
cart = CartModel();
cart.catalog = catalog;
}
}
or initialize them.
class MyStore extends VxStore {
CatelogModel catalog;
CartModel cart;
MyStore()
: catalog = CatelogModel(),
cart = CartModel(){
cart.catalog = catalog;
}
}
Author by
Dinesh Gosavi
Updated on December 20, 2022Comments
-
Dinesh Gosavi over 1 year
I am getting an error Non-nullable instance field 'catalog' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'
import 'package:first_app/models/cart.dart'; import 'package:first_app/models/catalog.dart'; import 'package:velocity_x/velocity_x.dart'; class MyStore extends VxStore { CatelogModel catalog; CartModel cart; MyStore() { #Getting error here# catalog = CatelogModel(); cart = CartModel(); cart.catalog = catalog; } }
-
xion about 3 yearsdeclare your catalog as nullable CatelogModel
CatelogModel? catalog;
-
-
Dinesh Gosavi about 3 yearsThanks the late keyword worked. but when i tried to initialize them it gave me error that The instance member 'catalog' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression.
-
eyoeldefare about 3 yearsYeah, thats because its not a property. I will update the answer to express the new change.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.