【问题标题】:The argument type 'Future' cant be assigned to the parameter type 'List<LatLng>'参数类型“Future”不能分配给参数类型“List<LatLng>”
【发布时间】:2020-03-22 05:48:38
【问题描述】:

我在我的 Flutter 项目中使用 Google 地图。我正在尝试在我的资产中解析一个 json 文件,以便我可以在折线中使用它的纬度和经度。我写了以下代码,当我把它变成未来时,如下:

  Future getPoints() async {
    List<dynamic> parsedJson = jsonDecode(
        'assets/busStops/stops_${widget.selectStation}.json');
    List<Marker> allMarkersByPosition = [];

    parsedJson.forEach((element) {
      List<dynamic> coords = element["bs"];

      coords.forEach((i) {
        double lat = double.tryParse(i["latitude"]);
        double lng = double.tryParse(i["longitude"]);

        return [
          LatLng(lat ?? 0.0, lng ?? 0.0),

        ];
      });
    });
  }

并在此处引用 getPoints():

    child: FutureBuilder(
        future:
            _future,
        builder: (context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            return CircularProgressIndicator();
          }
          List<dynamic> parsedJson = jsonDecode(snapshot.data);
          allMarkers = parsedJson.map((element) {
            return Marker(
                icon: customIcon,
                markerId: MarkerId(element["Latitude"].toString()),
                position: LatLng(element['Latitude'] ?? 0.0,
                    element['Longitude'] ?? 0.0));
          }).toList();

          return GoogleMap(
            initialCameraPosition: CameraPosition(
                target: LatLng(-26.1711459, 27.9002758), zoom: 9.0),
            markers: Set.from(allMarkers),
            onMapCreated: mapCreated,
            polylines: Set<Polyline>.of(<Polyline>[
              Polyline(
                polylineId: PolylineId('line'),
                points: getPoints(),
                visible: true,
                color: Colors.blue
              )
            ]),
          );
        },
      ),

我收到错误:参数类型“Future”不能分配给参数类型“List”

【问题讨论】:

    标签: json google-maps flutter dart polyline


    【解决方案1】:

    这是因为您的“getPoints”函数是一个async 函数,因此返回Future。这是正常的,这里没有什么问题。

    您可以通过将 GoogleMap 包装在 FutureBuilder 中来解决此问题(请查看此处:https://www.youtube.com/watch?v=ek8ZPdWj4Qo),并将“getPoints”作为参数传递给其函数,如下所示:

    return FutureBuilder(
          future: getPoints(),
          builder: (context, snapshot) {
    
            if (snapshot.connectionState == ConnectionState.waiting){
              return CircularProgressIndicator();
            }
    
            return GoogleMap(
              polylines: {
                Polyline(
                    polylineId: PolylineId('line'),
                    points: snapshot.data
                    visible: true,
                    color: Colors.blue
                    ),
              },
            );
          },
        );
    

    【讨论】:

    • 谢谢!但是我已经为我的标记创建了一个 Future builder,我可以添加另一个 Future 吗?
    • 当然可以。您可以根据需要拥有任意数量,但请注意在 initState 中声明它们,否则每次重建地图屏幕时都会重新创建它们。
    • 谢谢,我遇到了另一个相关问题:stackoverflow.com/questions/59065836/…
    【解决方案2】:

    第一种方法有问题。它永远不会真正返回任何东西。这是因为您在另一个方法中声明的方法中使用了 return。当您使用 forEach 时,您将传入一个方法,以应用于该列表中的每个元素。如果您在该方法中说返回,那么您只是从您传递给 forEach 的方法中返回。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2022-08-13
    • 1970-01-01
    • 2021-07-10
    • 2021-01-05
    • 2021-07-20
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多