Home > Back-end >  How do I resolve " type 'int' is not a subtype of type 'String' " with
How do I resolve " type 'int' is not a subtype of type 'String' " with

Time:10-11

I use the following code to make a list of menu items which I then want to display using ListView. This code populates the menuItemList correctly.

  Future<void> allMenuDetail(String orderID) async {
    _streamSubscriber =
        _dbRef.child('MenuItem/$orderID').onValue.listen((event) {
      if (event.snapshot.value != null) {
        var data = event.snapshot.value as Map;

        var price = data['price'];
        var itemName = data['itemName'];
        var desc = data['description'];
        var itemImg = data['img'];
        //  String itemID = data['id'];

        Map<String, dynamic> myMap = {
          'price': price,
          'itemName': itemName,
          'desc': desc,
          'img': itemImg,
          'itemID': itemID
        };
        List<dynamic> shortList = [myMap];
        menuItemList.addAll(shortList);
        setState(() {});
      } else {
      }
    });
  }

My issue is occurring in the ListView.

This is my code for the listView

          Expanded(
              child: ListView.builder(
            itemCount: menuItemList.length,
            itemBuilder: (context, index) {
              final item = menuItemList[index];//Error shows up here
              String price = item['price'];
              String itemName = item['itemName'];
              String desc = item['description'];
              String itemImg = item['img'];
              String itemID = item['itemID'];
          
              print('!');

              //   var cartPorducts = item['cartProducts'];
              return Container(
                  height: 95,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(24)),
                  margin: const EdgeInsets.fromLTRB(8, 10, 8, 10),
                  child: InkWell(
                      onTap: () {
                        Navigator.of(context).push(MaterialPageRoute(
                            builder: (context) => ItemPageState(
                                path: categoryPath,
                                itemName: itemName,
                                desc: desc,
                                price: 9,
                                itemImg: itemImg,
                                id: itemID)));
                        // }
                      },
                      child: Card(
                        margin: const EdgeInsets.fromLTRB(8, 10, 8, 10),
                        color: Colors.white,
                        clipBehavior: Clip.antiAlias,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(24),
                        ),
                        child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              ClipRect(
                                  child: Align(
                                alignment: Alignment.center,
                                widthFactor: 0.8,
                                child: Image(
                                  image: AssetImage(itemImg),
                                  height: 100,
                                  width: 150,
                                  fit: BoxFit.cover,
                                ),
                              )),
                              const SizedBox(width: 30),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    itemName,
                                    style: const TextStyle(
                                        color: Colors.black, fontSize: 25),
                                  ),
                                  Text(
                                    'Price: R', //  price.toString(),
                                    style: const TextStyle(
                                        color: Colors.black, fontSize: 18),
                                  ),
                                ],
                              ),
                              const SizedBox(width: 30),
                            ]),
                      )));
            },
          ))
        ]),

I have done it this way in other parts of may code and it worked fine but for some reason it does not seem to work in this instance. Where am I going wrong?

CodePudding user response:

Inside Your listView, price is int but you assign it to String so change it to this:

String price = (item['price'] as int).toString;

CodePudding user response:

Try this:

 String price = item['price'].toString();
  • Related