I am getting the data from firestore and storing the data as List, Since the data from the firestore is map i couldn't save it correctly and i am getting this error.
my code.
getData() async{
final dataDB = await FirebaseFirestore.instance.collection('TransactionData').doc(getUid()).get();
snapshot = dataDB;
final Map documentData = snapshot.data() as Map;
_cards = documentData as List?;
setState(() {
_data = _cards![0];
});
}
my data from Firestore.
{21Aug5298Sav: [{categoryName: Education, uid: LNM6JQuoKCPIANTyQHMKWLIzl5m1, amount: 3,000.00, lastUpdatedDate: 25-08-2022, createdDate: 25-08-2022, toAccountId: , type: Credit, transfer: N, accountId: 21Aug5298Sav, auto: Y, accountName: null, transactionId: deb1621, categoryLogo: assets/images/Education.png, untagged: N, categoryBackgroundColor: 0xffFFA2C0, currency: INR, transactionDate: 25-08-2022}, {categoryName: Education, uid: LNM6JQuoKCPIANTyQHMKWLIzl5m1, amount: 300.00, lastUpdatedDate: 25-08-2022, createdDate: 25-08-2022, toAccountId: , type: Debit, transfer: N, accountId: 21Aug5298Sav, auto: Y, accountName: null, transactionId: deb5366, categoryLogo: assets/images/Education.png, untagged: N, categoryBackgroundColor: 0xffFFA2C0, currency: INR, transactionDate: 25-08-2022}, {categoryName: Travelling, uid: LNM6JQuoKCPIANTyQHMKWLIzl5m1, amount: 100.00, lastUpdatedDate: 24-08-2022, createdDate: 24-08-2022, toAccountId: , type: , transfer: N, accountId: 21Aug5298Sav, a<…>
i just want the list to contain all the data which was retrieved from the firestore. Can someone please help me on this.
Thanks in advance.
CodePudding user response:
You cant cast a Map to a List. Thats you error. If you check your data, you will see that is not a List, it's a Map. If you want the list inside, you will need to access through the key "21Aug5298Sav"
Ex.
List _cards = documentData["21Aug5298Sav"]
CodePudding user response:
In the codes you had tried to cast a map as a list and assign it to the_cards variable. Try this
final Map documentData = snapshot.data() as Map;
_cards = documentData.values.toList();
//Or
_cards = documentData.values.toList().first;
Edit
List<Map<String, dynamic>> _cards = [];
var allDocData = documentData.values.toList();
for(int i= 0; i < allDocData.length; i )
{
_cards.add(allDocData[i] as Map<String, dynamic>);
}
CodePudding user response:
try this:
Map data = snapshot.data() as Map;
var firestoreResult = documentData.values.toList();
var finalResult = [];
for (var item in firestoreResult) {
for (var x in item) {
finalResult = [...finalResult, ...x];
}
}
and use it like this:
setState(() {
_data = finalResult![0];
});