Home > OS >  Unable to retrieve firebase data to Listview
Unable to retrieve firebase data to Listview

Time:02-22

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

The Firebase Json Tree: enter image description here

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.

CodePudding user response:

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.

CodePudding user response:

You have to Quantity make a String value, use toString() method on it in Text Widget Like that:

"Text("Quantity: "   lists[index]["Quantity"].toString())"
  • Related