Home > database >  How to cast Map<String,List<dynamic>> to Map<String, List<DetailedPositions>>
How to cast Map<String,List<dynamic>> to Map<String, List<DetailedPositions>>

Time:01-22

I'm reading records from PostgreSQL and group cust_id level, resulted type is Map<String, List>. Trying to convert Map<String,List<dynamic>> to Map<String, List<DetailedPositions>> but facing Exception in fetchDetailedPositions type _PostgreSQLResultRow' is not a subtype of type 'Map<String, dynamic>'.toString() any help would be appriciated.

  Future<Map<String, List<dynamic>>> fetchDetailedPositions(
      String custID) async {
      await connection!.open();
      await connection!.transaction((fetchDataConn) async {
        _fetchMasterPositionData = await fetchDataConn.query(
          "SELECT cust_id, cust_name, net_quantity FROM positions");

        if (_fetchMasterPositionData!.affectedRowCount > 0) {
          fetchDataFuture = _fetchMasterPositionData!.toList(growable: true);
          Map<String, List<dynamic>> groupedPositions = groupBy(
              fetchDataFuture, (item) => item[0].toString()   ','   item[1]);

          Map<String, List<DetailedPositions>> newMap = {};
          groupedPositions.forEach((key, value) {
            newMap[key] =
                value.map((e) => DetailedPositions.fromJson(e)).toList();  **--> code challenge here**
          });
          }});
    return newMap;
  }

class DetailedPositions {
  String custID;
  String custName;
  int quantity;

  DetailedPositions(...)

  factory DetailedPositions.fromJson(Map<String, dynamic> json) {
    return DetailedPositions(
      custID: json['custID'] as String,
      custName: json['custName'] as String,
      quantity: json['quantity'] as int,
    );}}

CodePudding user response:

change this

 Map<String, List<dynamic>> groupedPositions

to

 Map<String, List<DetailedPositions>> groupedPositions

CodePudding user response:

Try this code this will help you

newMap= groupedPositions.map((key, value) {
  return MapEntry(key, value.map((e) => DetailedPositions.fromJson(e)).toList());
},);
  • Related