How to resolve non-nullable error in Flutter? How to resolve non-nullable error in Flutter? flutter flutter

How to resolve non-nullable error in Flutter?


You must initialize the CategoryModel.categories variable, since this variable has been defined as a non-nullable it cant be left uninitialized as this is null.

You should update the model class and set an initial value for the categories, for example

class CategoryModel {     static List<Category> categories = List.empty();}


Your variable static List<Category> categories; is a List<Category> type. Since its non-nullable, the value it holds can never be null. So from the initialization you need to supply a value.

You can solve this in a few ways. The easiest would be to give an initial value:

static List<Category> categories = [];

Other ways to handle this without giving an initial value would be to make the variable nullable, or marking it with a late initialization keyword. To read more of this, check the oficial documentation here.