简体   繁体   中英

Unable to retrieve firebase data to Listview

I am trying to retrieve data from my real-time firebase into a listview.

The Firebase Json Tree: 在此处输入图像描述

I want to retrieve Item, Expiry Date and Quantity in the listview format.

My code is as follows:

var lists = [];
 final database = FirebaseDatabase(
      databaseURL: "https://trackkit-a5cf3-default-rtdb.asia-southeast1.firebasedatabase.app")
      .reference()
      .child('Location 1');

@override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Container(
        child: StreamBuilder(
          stream: database.onValue,
          builder: (context, AsyncSnapshot<Event> snapshot) {
            if (snapshot.hasData && !snapshot.hasError &&
                snapshot.data!.snapshot.value != null) {
              print("Error on the way");
              lists.clear();
              DataSnapshot dataValues = snapshot.data!.snapshot;
              Map<dynamic, dynamic> values = dataValues.value;
              values.forEach((key, values) {
                lists.add(values);
              });
              return  ListView.builder(
                shrinkWrap: true,
                itemCount: lists.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("Item: " + lists[index]["Item"]),
                        Text("Expiry Date: " + lists[index]["Expiry Date"]),
                        Text("Quantity: " + lists[index]["Quantity"]),
                      ],
                    ),
                  );
                },
              );
            }
            return Container(child: Text("Add Items"));
          },
        ),
      ),

    );
  }

The current screen shows: type 'int' is not a subtype of type 'String'

I have been trying for days. Any help would be greatly appreciated.

Maybe you are getting a Integer value in return to your lists[index]["Quantity"] . And you cannot concat and integer with String without converting into a String.

So Please use method toString() with lists[index]["Quantity"] to make it work properly.

You have to Quantity make a String value, use toString() method on it in Text Widget, like that: Text("Quantity: " + lists[index]["Quantity"].toString())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM