【问题标题】:type 'Future<Stations>' is not a subtype of type 'Iterable<dynamic>''Future<Stations>' 类型不是 'Iterable<dynamic>' 类型的子类型
【发布时间】:2021-09-13 10:05:28
【问题描述】:

API 调用由按钮按下触发,此按钮调用 AnchoredMapMarkers 应在预加载的地图上显示纬度/经度图钉

async 之后返回print('step5.1_APICALL');

错误显示为type 'Future&lt;Stations&gt;' is not a subtype of type 'Iterable&lt;dynamic&gt;'

非常感谢任何帮助

API 调用并以列表形式正常显示 - 我现在尝试从 Json 中提取纬度/经度并将其绘制在我的地图 SDK 上

class MapMarkerExample{
  
    
 void showAnchoredMapMarkers() { 
  var stations;
       stations = fetchStations(); 
       for (Station stations in stations) {
           GeoCoordinates geoCoordinates = GeoCoordinates (stations.place.location.lat, stations.place.location.lng);
       
           _addCircleMapMarker(geoCoordinates, 0);
           _addPOIMapMarker(geoCoordinates, 1);
           
           } 
        }


  Future<Stations> fetchStations() async {
  print('step5.1_APICALL');
    var client = http.Client();

      final response = await client.get(
      'https://transit.hereapi.com/v8/stations?in=lat,-long&return=transport&apiKey=API_KEY');
   
    if (response.statusCode == 200) {
    return Stations.fromJson(jsonDecode(response.body)); 
    } else {
    throw Exception('Failed to load stations');
    }
  }

 

【问题讨论】:

    标签: ios flutter dart mobile


    【解决方案1】:

    你不是在等待你的未来。为了使编译器更加复杂,您以某种方式拆分声明和赋值。并重用了一个变量名。

    修复:

    Future<void> showAnchoredMapMarkers() async { 
        final stations = await fetchStations();
    
        for (Station station in stations) {  
        ... 
    

    【讨论】:

    • 谢谢,错误已经消失。唯一的问题是地图标记没有显示在地图上 - 虽然没有记录错误
    • 这是现在的反馈_AsyncAwaitCompleter.complete (dart:async-patch/async_patch.&lt;…&gt; [VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: Unable to load asset: assets/poi.png
    • 嗯,错误信息很清楚,不是吗?您没有该名称的资产,或者您可能没有在您的 yaml 文件中列出它
    【解决方案2】:

    你需要等待

    Future<void> showAnchoredMapMarkers() async {
      var stations = await fetchStations();
      for (Station stations in stations) {
        GeoCoordinates geoCoordinates = GeoCoordinates (stations.place.location.lat, stations.place.location.lng);
        
        _addCircleMapMarker(geoCoordinates, 0);
        _addPOIMapMarker(geoCoordinates, 1);
      } 
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2020-09-11
      • 2020-10-13
      • 2021-02-10
      • 1970-01-01
      • 2020-01-14
      • 2021-08-29
      • 2020-01-04
      • 2023-03-27
      相关资源
      最近更新 更多