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.
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<...>(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With