Cherkaoui Jamal
Cherkaoui Jamal

Reputation: 1

reading firestore array field on Flutter list builder

Reading Firestore array field on a Flutter list builder. Here the code I tried. But I got this error message :

enter image description here

please help!

class _ListClients2 extends State<ListClients2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: FutureBuilder(
      future: getList(),
      builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else {
          return Center(
            child: ListView.builder(
                padding: const EdgeInsets.only(bottom: 20.0),
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: snapshot.data.length,
                itemBuilder: (context, index) {
                  return Center(
                    child: ListTile(
                      title: Text(snapshot.data[index].data
                          .toString()), //snapshot data should dispaly in this text field
                    ),
                  );
                }),
          );
        }
      }
   
  Future<List<dynamic>> getList() async {
    var firestore = Firestore.instance;

    DocumentReference docRef =
        firestore.collection('admin_users').document(idUser);

   
    return docRef.get().then((datasnapshot) {
      if (datasnapshot.exists) {
        List<dynamic> info = datasnapshot.data['clients'].toList();
        print('#');
        print(info); 
        print(info.length); 
        return info;
      }
    });
  }

Upvotes: 0

Views: 280

Answers (2)

Samuel Romero
Samuel Romero

Reputation: 1263

The "NoSuchMethodErrorr: Class" error most of the times mean that you are trying to read a data type instead of the data type you are sending. So, it seems like you are expecting the "clients" part of the datasnapshot to be a list but it's apparently a string.

I would recommend you to double check the current datasnapshot text and probably you have something like

"clients": "something here" 

instead of

"clients": [...]

Upvotes: 0

Harsha pulikollu
Harsha pulikollu

Reputation: 2436

You have to provide data()['clients'] not data['clients'] to get value from document.

List<dynamic> info = datasnapshot.data()['clients'].toList();

Upvotes: 1

Related Questions