Steven J Reeves
Steven J Reeves

Reputation: 101

How do you get a a collection of documents from firestore and turn it into a list in flutter?

I have been trying to figure out how to query Firestore and save the output to a list so that I can use it in another part of the app.

I have tried to save it into a list but I get "Unhandled Exception: type 'double' is not a subtype of type 'List dynamic'".

I then tried to save it as a List but then I get "Unhandled Exception: type 'double' is not a subtype of type 'List double'

I also tried to output it as a map but I think I'm missing something there because it won't compile.

Here is the function:

setMainDebt()async{
    final uid = await Provider.of(context).auth.getCurrnetUid();
    final fireStoreData = await db.collection('userDebts').document(uid).collection('debts').orderBy('balance').getDocuments();
    for(var theData in fireStoreData.documents){
        final List<double> value = theData.data['balance'];
        print(value);    
    }    
  }

Upvotes: 0

Views: 67

Answers (3)

Habib Mhamadi
Habib Mhamadi

Reputation: 769

I think you are trying to assign a double value directly to a List. you should add them instead.

List<double> list=[];

final fireStoreData = await db.collection('userDebts').document(uid).collection('debts').orderBy('balance')

fireStoreData.getDocuments().then((val)=>{ val.documents.forEach((doc)=>{ list.add(doc.data['balance']) }) });

Upvotes: 1

Jagraj Singh
Jagraj Singh

Reputation: 4391

You can make a List<double> and then add data in it and then use it where you want. You are currently getting error because you can't assign a double type value in aListtype

Try:

Future<List<double>>setMainDebt()async{
    List<double> list=[]; // make an empty list
    final uid = await Provider.of(context).auth.getCurrnetUid();
    final fireStoreData = await db.collection('userDebts').document(uid).collection('debts').orderBy('balance').getDocuments();
    for(var theData in fireStoreData.documents){
        list.add(theData.data['balance'] as double); // typecast data as double and add it in list
        print(value);    
    }    
    return list; // return list
  }

Upvotes: 0

Siddharth Agrawal
Siddharth Agrawal

Reputation: 3146

It seems like when you call theData.data['balance'] (which can be a DocumentReference not var), you get a list of double values, not a double value. Check your database. You must be storing it as a list. To overcome that, edit your code like this.

 for(var theData in fireStoreData.documents){
      for (List l in theData.data['balance']){
        final List<double> value = l;
        print(value);    
    }    
}

Upvotes: 0

Related Questions