Home > Software design >  LateInitializationError: Field 'items' has not been initialized
LateInitializationError: Field 'items' has not been initialized

Time:11-02

This error pops up in my app whenever I try to navigate from login page to home page.

Screenshot of the error

Debug console in vs code shows error like this.

═════════════ Exception caught by widgets library ═════════════

The following LateError was thrown building HomePage(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#d5a11]], state: _HomePageState#10e51):

LateInitializationError: Field 'items' has not been initialized.

The relevant error-causing widget was
HomePage
lib\main.dart:22
When the exception was thrown, this was the stack

**#0      CatalogModel.items (package:flutter_catalog/models/catalog.dart)
package:flutter_catalog/models/catalog.dart:1
#1      _HomePageState.build and so on.....**

My catalog.dart page

   class CatalogModel {
   static late List<Item> items;

  // Get Item by ID
  Item getById(int id) =>
      items.firstWhere((element) => element.id == id, orElse: null);

  // Get Item by position
  Item getByPosition(int pos) => items[pos];
}

class Item {
  final int id;
  final String name;
  final String desc;
  final num price;
  int quantity;
  final String color;
  final String image;

  num get totalPrice => price * quantity;

  Item({
    required this.id,
    required this.name,
    required this.desc,
    required this.price,
    required this.color,
    required this.image,
    this.quantity = 1,
  });

  Item copyWith({
    int? id,
    String? name,
    String? desc,
    num? price,
    String? color,
    int? quantity = 1,
    String? image,
  }) {
    return Item(
      id: id ?? this.id,
      name: name ?? this.name,
      desc: desc ?? this.desc,
      price: price ?? this.price,
      color: color ?? this.color,
      image: image ?? this.image,
      quantity: quantity ?? this.quantity,
    );

}

HomePage.dart

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  
  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    await Future.delayed(Duration(seconds: 2));
    final catalogJson =
        await rootBundle.loadString("assets/files/catalog.json");
    final decodedData = jsonDecode(catalogJson);
    var productsData = decodedData["products"];
    CatalogModel.items = List.from(productsData)
        .map<Item>((item) => Item.fromMap(item))
        .toList();
    (VxState.store as MyStore).items = CatalogModel.items;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    final _cart = (VxState.store as MyStore).cart;
    final MyStore store = VxState.store;
    return Scaffold(
        backgroundColor: context.canvasColor,
        floatingActionButton: VxBuilder(
          mutations: {AddMutation, RemoveMutation},
          builder: (ctx,_,status) =>FloatingActionButton(
          onPressed: () =>
              store.navigator.routeManager.push(Uri.parse(MyRoutes.cartRoute)),
          backgroundColor: context.theme.buttonColor,
          child: Icon(
            CupertinoIcons.cart,
            color: Colors.white,
          ),
        ).badge(
              color: Vx.red500,
              size: 22,
              count: _cart.items.length,
              textStyle: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
              )),
        ),
        body: SafeArea(
          child: Container(
            padding: Vx.m32,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CatalogHeader(),
                CupertinoSearchTextField(
                  onChanged: (value) {
                    SearchMutation(value);
                  },
                ).py12(),
                **if (CatalogModel.items !=null && CatalogModel.items.isNotEmpty)**
                  CatalogList().py16().expand()
                else
                  CircularProgressIndicator().centered().expand(),
              ],
            ),
          ),
        ));
  

}
}

Cart.dart

  class CartModel {
  late CatalogModel _catalog;

  final List<int> _itemIds = [];
  CatalogModel get catalog => _catalog;

  set catalog(CatalogModel newCatalog) {
    assert(newCatalog != null);
    _catalog = newCatalog;
  }
  List<Item> get items => _itemIds.map((id) => _catalog.getById(id)).toList();
  num get totalPrice =>
      items.fold(0, (total, current) => total   current.price);


}

class AddMutation extends VxMutation<MyStore> {
  final Item item;

  AddMutation(this.item); 
 @override
  perform() {
    store!.cart._itemIds.add(item.id);
    
  }
}
 
 class RemoveMutation extends VxMutation<MyStore> {
  final Item item;

  RemoveMutation(this.item); 
 @override
  perform() {
    store!.cart._itemIds.remove(item.id);
    
  }
}

Please help me this code so that no error as stated above(screenshot) pops up whenever I try to navigate from one page to another. [Also, I tried to fix the error by removing static late , @required keyword but nothing worked and ended up getting more error.Please review the code and help me with the same.]

CodePudding user response:

You are accessing items on loading cases, It will take some time to fetch it.

 Widget build(BuildContext context) {
    final _cart = (VxState.store as MyStore).cart; /// here trying to get on 1st frame

It would be better to use FutureBuilder. Or provide empty list instead of late or make it nullabel.

 static  List<Item> items = [];

Check the FutureBuilder

  • Related