简体   繁体   中英

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>?' in type cast

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.

截屏

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

var allDocData = documentData.values.toList();

for(int i= 0; i < allDocData.length; i++)
 {
   _cards.add(allDocData[i]);
}


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"]

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];
});

I have changed the Data structure of my firestore data and its working perfectly. Thanks guys for your valuable time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM