Flutter:InternalLinkedHashMap <string,dynamic =“”>'没有实例方法'cast'具有匹配的参数

时间:2019-01-24 10:27:19

标签: android api http dart flutter

我无法从先前可用的问题中找到解决方案,我已将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方法

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.

1 个答案:

答案 0 :(得分:1)

改为使用

.cast<String,dynamic>();

另请参阅https://api.dartlang.org/stable/2.0.0/dart-core/Map/cast.html

通常最好使用Map<String,String>.from(oldMap)而不是cast<...>(...)