Convert Firestore DocumentSnapshot to Map in Flutter

后端 未结 5 1621
误落风尘
误落风尘 2020-12-16 07:13

I need to update a document with nested arrays in Firestore with Flutter.

So I need to get the full document into a Map, reorder the maps in the \"sections\" array,

5条回答
  •  萌比男神i
    2020-12-16 07:51

    Needed to simplify for purpose of example

    class ItemsList extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // get the course document using a stream
        Stream courseDocStream = Firestore.instance
            .collection('Test')
            .document('4b1Pzw9MEGVxtnAO8g4w')
            .snapshots();
    
        return StreamBuilder(
            stream: courseDocStream,
            builder: (context, AsyncSnapshot snapshot) {
              if (snapshot.connectionState == ConnectionState.active) {
    
                // get course document
                var courseDocument = snapshot.data.data;
    
                // get sections from the document
                var sections = courseDocument['sections'];
    
                // build list using names from sections
                return ListView.builder(
                  itemCount: sections != null ? sections.length : 0,
                  itemBuilder: (_, int index) {
                    print(sections[index]['name']);
                    return ListTile(title: Text(sections[index]['name']));
                  },
                );
              } else {
                return Container();
              }
            });
      }
    }
    

    The Results

提交回复
热议问题