代码之家  ›  专栏  ›  技术社区  ›  Farhana Naaz Ansari Jay Dwivedi

颤振:InternalLinkedHashMap<String,dynamic>”没有具有匹配参数的实例方法“cast”

  •  1
  • Farhana Naaz Ansari Jay Dwivedi  · 技术社区  · 7 年前

    我无法从先前可用的问题中找到解决方案,我已投下 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 回复  |  直到 7 年前
        1
  •  19
  •   Günter Zöchbauer    7 年前

    .cast<String,dynamic>();
    

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

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