Dinesh Gosavi
Dinesh Gosavi

Reputation: 21

How to solve Non-nullable instance field 'catalog' must be initialized. Try adding an initializer expression,?

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;
  }
}

Upvotes: 2

Views: 3783

Answers (2)

Non-nullable instance field 'audioPlayer' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.

Upvotes: -1

eyoeldefare
eyoeldefare

Reputation: 2303

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;
        }
}

Upvotes: 2

Related Questions