一尘不染

Flutter:InternalLinkedHashMap'没有实例参数'cast'具有匹配的参数

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.


阅读 845

收藏
2020-08-13

共1个答案

一尘不染

改用

.cast<String,dynamic>();

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

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

2020-08-13