我无法从先前可用的问题中找到解决方案,我已将 json字符串强制转换为map
json
map
下面是我的API调用方法。
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; }
这是我的convertEventResponse方法
convertEventResponse
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, ); }
我得到的错误。
InternalLinkedHashMap<String, dynamic>' has no instance method 'cast' with matching arguments.
InternalLinkedHashMap<String, dynamic>' has no instance method 'cast'
with matching arguments.
改用
.cast<String,dynamic>();
另请参阅https://api.dartlang.org/stable/2.0.0/dart- core/Map/cast.html
通常,最好使用Map<String,String>.from(oldMap)而不是cast<...>(...)
Map<String,String>.from(oldMap)
cast<...>(...)