Retrieve Map Data in Firebase to Flutter

Viewed 3329

I have this data structure Database Structure

I need to call all the item in the FavDestination collection and here is my code in Flutter

child: StreamBuilder(
stream: Firestore.instance.collection('FavDestination').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData) return Text('Loading Data... Please Wait');
return
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length, //snapshot.data.document.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot destination = snapshot.data.documents[index];
//DocumentSnapshot destination = snapshot.data.document[index];
return GestureDetector(
                  onTap: () => Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) => DestinationScreen(
                        destination: destination,
                      ),
                    ),
                  ),

It retrieves the data successfully, and if the user taps on the document, it will go to Destination Screen and I need to retrieve all the data in activities, here is my code in the Destination Screen

Expanded(
        child: ListView.builder(
          padding: EdgeInsets.only(top: 10.0, bottom: 15.0),
          itemCount: widget.destination['activities'].length,
          itemBuilder: (BuildContext context, int index) {
            DocumentSnapshot activity = widget.destination['activities'][index];
            return Stack(
              children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                       Container(
                          width: 120.0,
                          child: Text(
                          activity['name'],
                          style: TextStyle(
                          fontSize: 18.0,
                          fontWeight: FontWeight.w600,
                          ),

and here is the Error I got Error Message

any idea how to solve it? Thank you

1 Answers

It seems that your activities field is nested map. On first level you have field Bali1 which is map as well. And it the code activity is trying to get name on first level.

I suppose you should work more on destination object structure. You can print it out to analyze and try to map object like Map<dynamic, dynamic> and Bali1 as well.

I don't have playground to do it fast, but I found similar approach here: How do I get specific values from a datasnapshot in flutter?

I hope it will help!

Related