Home > Back-end >  Flutter firestore how get document one by one?
Flutter firestore how get document one by one?

Time:12-21

There are documents in the collection in Firestore. I list these documents on the screen according to the user's email address. But I want to import each document separately. Each document has an array called title. When I want to get the array of each document separately, I get the following error.

CODE:

Widget getCustomerRepairLog() {
final user = FirebaseAuth.instance.currentUser!; //better do a null check 1st
late final _repairLogs =
  FirebaseFirestore.instance.collection('Repair').get();
return FutureBuilder(
  future: _repairLogs,
  builder: (context, AsyncSnapshot snapshot) {
    if (snapshot.hasData) {
      List<Widget> items = [];
      final data = snapshot.data?.docs;
      if (data == null) return Text("got null");

      for (final eleman in data) {
        if (eleman.data()['email']! == user.email) {
          for (final item in eleman.data()['title']) {
            items.add(Text("${item}"));
            
          }
          return Column(crossAxisAlignment: CrossAxisAlignment.start,children: 
   [Text(eleman)]);
        }
      }
      // return Row(children: [Icon(Icons.done_all_rounded), ...items]);
      // return Column(crossAxisAlignment: CrossAxisAlignment.start,children: [...items]);
      
      // return CircularProgressIndicator();
    }
    return CircularProgressIndicator();
  });
  }

ERROR: type '_JsonQueryDocumentSnapshot' is not a subtype of type 'String'

CodePudding user response:

Try the following code solution:

Widget getCustomerRepairLog() {
final user = FirebaseAuth.instance.currentUser!; //better do a null check 1st
late final _repairLogs =
  FirebaseFirestore.instance.collection('Repair').get();
return FutureBuilder<QuerySnapshot>(
  future: _repairLogs,
  builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasData) {
      List<Widget> items = [];
      final data = snapshot.data?.docs;
      if (data == null) return Text("got null");

      for (final eleman in data) {
        final currentData  = eleman.data() as Map<String, dynamic>;
        if (current['email']! == user.email) {
          for (final item in current['title']) {
            items.add(Text("${item}"));
            
          }
          return Column(crossAxisAlignment: CrossAxisAlignment.start,children: 
   [Text(current.toString())]); // this represents Map<String, dynamic> 
        }
      }
      // return Row(children: [Icon(Icons.done_all_rounded), ...items]);
      // return Column(crossAxisAlignment: CrossAxisAlignment.start,children: [...items]);
      
      // return CircularProgressIndicator();
    }
    return CircularProgressIndicator();
  });
  }

here I change elemant in the Text widget which is a whole DocumentSnaphot with its data which is Map<String, dynamic> :

  Text(element); // this represents DocumentSnapshot which is not a String so it throws the error

  Text(currentData.toString()); // this represents the document's Map<String, dynamic> data which I make it a String with the toString()

CodePudding user response:

Try the following code:

ListView.builder(
  itemCount: data.length,
  itemBuilder: (context, index) {
    if (data[index].data()['email'] == user.email) {
      final item = data[index].data()['title'];
      return Text("${item}");
    }
    return Container();
  },
),
  • Related