Mohsin
Mohsin

Reputation: 137

Flutter Trying to convert <List<Map<String, dynamic>>> to List<Transactions>

I'm trying to convert <List<Map<String, dynamic>>> to List
I run a query operation:

   static Future<List<Map<String, dynamic>>> getData(String table) async {
      final db = await DBHelper.database();
      return db.query(table);
   }

And calling that method into another class like this:

Future<void> fetchAndSetData() async {
    final dataList = await DBHelper.getData('history');
    print(dataList);
    _transactions = dataList
        .map(
          (item) => Transaction(
            id: int.parse(item['id']),
            senderEmail: item['senderEmail'],
            receiverEmail: item['receiverEmail'],
            amount: item['amount'],
          ),
        )
        .toList();
    print('Data: $_transactions');
    notifyListeners();
}

After print(dataList). I got the data into the console as expected:

[{id: 1, senderEmail: [email protected], receiverEmail: [email protected], amount: 2000.0}, {id: 2, senderEmail: [email protected], receiverEmail: [email protected], amount: 2000.0}]

But when I printed print('Data: $_transactions'); There is no logs. _transactions is initialized by an empty array like this List _transactions = [];

I expect to get the List to contains Transactions.

I'm trying to convert the <List<Map<String, dynamic>>> into List but it is not working, also there is no error. My Transaction class looks like this:

class Transaction {
  final int id;
  final String senderEmail;
  final String receiverEmail;
  final double amount;

  Transaction({
    this.id,
    this.senderEmail,
    this.receiverEmail,
    this.amount,
  });
}

Upvotes: 0

Views: 1866

Answers (1)

Ravi Singh Lodhi
Ravi Singh Lodhi

Reputation: 2793

Try adding fromMap & parseTransactionList methods to your Transaction model class.

class Transaction {
  final int id;
  final String senderEmail;
  final String receiverEmail;
  final double amount;

  Transaction({
    this.id,
    this.senderEmail,
    this.receiverEmail,
    this.amount,
  });
  
  factory Transaction.fromMap(Map<String, dynamic> map) {
    return Transaction(
      id: int.parse(map['id']),
      senderEmail: map['senderEmail'],
      receiverEmail: map['receiverEmail'],
      amount: map['amount'],
    );
  }
  
  static List<Transaction> parseTransactionList(List<dynamic> list) {
    if (list == null) return null;
    
    final transactionList = <Transaction>[];
    for (final item in list) {
      transactionList.add(Transaction.fromMap(item));
    }
    
    return transactionList;
  }
}

And to get the transaction list you can do as follows:

Future<void> fetchAndSetData() async {
    final dataList = await DBHelper.getData('history');
    print(dataList);
    _transactions = Transaction.parseTransactionList(dataList);
    print('Data: $_transactions');
    notifyListeners();
}

You can pass each item(Map<String, dynamic>) & create a Transaction object from it & add it to the list. This is a very common practice usually to convert json objects to model class object.

Upvotes: 1

Related Questions