4
votes

I'm little bit stuck on some situation.

1) type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'

My code.

List<Map<String, dynamic>> arrayOfProductList = List<Map<String, dynamic>>();

Calling APIs

Future<Map<String, dynamic>> _callWebServiceForGetProductList() async{

    String strURL = 'http:xxxxxx/productlist.php';
    Map<String, String> headerDic = {"Authorization": "ff624bc580ab48a3910d4352dxxx712"};
    Map<String, String> paramDic = {"page": "1", "search": "", "term_id": ""};

    http.Response httpRes = await http.post(strURL, headers: headerDic, body: paramDic);
    Map<String, dynamic> responseDic = json.decode(httpRes.body);
    return responseDic;
  }

FutureBuilder Widget and stuff

Expanded(
              child: Container(
                child: FutureBuilder(
                    future: _callWebServiceForGetProductList(),
                    builder: (BuildContext context, AsyncSnapshot snap) {
                    if(snap.hasData) {
                      Map<String, dynamic> dicOfData = snap.data;
                      arrayOfProductList = dicOfData["data"] as List<Map<String, dynamic>>;
                      print(arrayOfProductList);
                      _setupProductListView();
                    }
                    else if(snap.hasError) {
                      showDialog(context: context, builder:(_) => AlertDialog(
                        content: Text("Error ${snap.hasError.toString()}"),
                        title: Text("Error"),
                      ));
                    }
                    return Center(child: CircularProgressIndicator());

                    }
                ),
              ),
            )

I'm googling for last 2 hours but didn't get any result. Where i'm going wrong? Guide me.

1
Try reading the error message. _InternalLinkedHashMap is the same as Map (just an internal representation). The value is a Map<String,dynamic> and the type you try to assgn to is a List<Map<String,dynamic>>. So you expect a list bat the actual value is not a list, it's a single value. - Günter Zöchbauer
@GünterZöchbauer Sorry I'm new at Flutter. I know that can't able to cast the list but how can I solve it. Means Map<String, dynamic> has key ..name is "data" and This key has List<Map<String, dynamic>> (Array of dictionary). How should I write it? - Govaadiyo
So instead of declare List<Map<String, dynamic>>. I have to declare just var arrayOfProductList; ? - Govaadiyo
Can you please post single page app demo with simple working api call? - jignesh
try using as List without generic definition <Map<String,dynamic>> just remove it ,let me know if this solved the issue - Saed Nabil

1 Answers

0
votes

The cast attempt on dicOfData["data"] as List<Map<String, dynamic>> seems to have caused the type mismatch error you received. If you can provide more details on the data the snapshot contains, this can help us identify why it causes the error.