Home > Blockchain >  type 'List<Map<String, dynamic>>' is not a subtype of type 'Map<dynami
type 'List<Map<String, dynamic>>' is not a subtype of type 'Map<dynami

Time:08-29

I am trying to set the from List to Map i got this error.

I am getting the data from the Firestore, Below you can see the query and the output.

  List<Map<String, dynamic>> transactionData = [];
  List accountList = [];
  List availableAccount = [];

getData() async{
    final dataDB = await FirebaseFirestore.instance.collection('TransactionData').doc(getUid()).get();
    snapshot = dataDB;
    final Map<String, dynamic> documentData = snapshot.data() as Map<String, dynamic>;

    documentData.forEach((key, value) {
      availableAccount.add(key);
    });

    for(int j=0;j<availableAccount.length;j  ){
      transactionData = (documentData[availableAccount[j]] as List).map((accountList) => accountList as Map<String, dynamic>).toList();
      accountList.add(transactionData);
    }
    _cards = accountList;
    print('Cards: $_cards');
    setState(() {
      _data = _cards![0];
    });
  }
Cards: [[{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, auto: Y, accountName:<…>

can someone please help me how to resolve this, i am getting error when i set '_cards![0]' to '_data'.

screenshot

CodePudding user response:

You expect _cards be a List<Map<String, dynamic>> but it is not. It is a List<List<Map<String, dynamic>>> and try to pass first item of it which is List<Map<String, dynamic>> to _data which is Map<String, dynamic> and this is a problem. If you wanna pass any value from _cards to _data should be like this:

_data = _cards![0][0];

if you want convert List<List<Map<String, dynamic>>> to List<Map<String, dynamic>>, do this:

List<String> newCards = [];
          
for (var item in _cards) {
  newCards = [...newCards, ...item];
}

and then:

_data = newCards[0]; 
  • Related