skip to Main Content

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.

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

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();
    },
  );
}

2

Answers


  1. 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 (currentData  ['email']! == user.email) {
              for (final item in currentData['title']) {
                items.add(Text("${item}"));
                
              }
              return Column(crossAxisAlignment: CrossAxisAlignment.start,children: 
       [Text(currentData.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()
    
    Login or Signup to reply.
  2. 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();
      },
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search