Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments

I'm unable to find solutions from the previously available question, I have cast json string to map

Below is my API calling method.

Future<EventResponse> fetchEvent( ) async { // here i change Future type

 String url='http://xxxxxxxxxxxx.tk/api/userapp/event/lists';

 var headers = new Map<String, String>();//here i defined Map type
 headers['Auth-Key'] = 'OCDOC@2018';
 headers['End-Client'] = 'OCDOC';

 var body = new Map<String, String>();//here i defined Map type
 headers['schedule'] = 'present';

 http.Response res = await http.post(url,headers: headers, body: body);

 final parsed=json.decode(res.body);
 var myMap = Map<String, dynamic>.from(parsed);
 EventResponse eventResponse = EventResponse.convertEventResponse(myMap);
 return eventResponse;


 }

this is my convertEventResponse methode

factory EventResponse.convertEventResponse(Map<String, dynamic> json) {
    List<dynamic> events = json['eventList'];
    List<Event> eventList = events.map((e) => Event.convertEvent(e)).toList(); //here i changed by @Richard Heap answer
    return EventResponse(
      error: json['error'],
      status: json['status'],
      deliveryCharges: json['deliveryCharge'],
      imageBaseUrl: json['image_base_url'],
      imageLogoUrl: json['image_logo_url'],
      eventList: eventList,
    );
  }

The error i'm getting.

InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
like image 556
Farhana Naaz Ansari Avatar asked Jan 24 '19 10:01

Farhana Naaz Ansari


1 Answers

Use instead

.cast<String,dynamic>();

See also https://api.dartlang.org/stable/2.0.0/dart-core/Map/cast.html

Usually it's better to use Map<String,String>.from(oldMap) instead of cast<...>(...)

like image 111
Günter Zöchbauer Avatar answered Sep 27 '22 00:09

Günter Zöchbauer