【问题标题】:Convert Future<int> to int in Flutter在 Flutter 中将 Future<int> 转换为 int
【发布时间】:2020-03-25 00:41:39
【问题描述】:

我的目标是根据用户的地理位置对机架列表进行排序。这是我的代码:

import 'package:geolocator/geolocator.dart';
import 'rack.dart';
import 'dart:async';
class RackList {

  List<Rack> _racks;
  RackList(this._racks);
  get racks => _racks;

  factory RackList.fromJson(Map<String,dynamic> json){
    var list = json['racks'] as List;

    Future<int> calculateDistance(var element) async{
      var startLatitude=45.532;
      var startLongitude=9.12246;
      var endLatitude=element.latitude;
      var endLongitude=element.longitude;
      var dist = await Geolocator().distanceBetween(
          startLatitude,
          startLongitude,
          endLatitude,
          endLongitude);
      var distance=(dist/1000).round();
      return distance;
    }

    list = list.map((i) => Rack.fromJson(i)).toList();

      for (int i = 0; i < list.length; i++) {
        var dist =  calculateDistance(list[i]);
        print(dist); //prints Instance of 'Future<int>'
        list[i].distance=dist; //crash
    }

    list.sort((a, b) => a.distance.compareTo(b.distance));
    return RackList(list);
  }
}

问题出在for循环中,变量distFuture&lt;int&gt;类型,不能赋值给list[i].distance。如何将该值转换为普通 int?

我已经尝试过@Nuts 的解决方案,但是:

var distances = new List();
for (int i = 0; i < list.length; i++) {
      calculateDistance(list[i]).then((dist) {
        distances.add(dist);
        print(distances[i]); //print the correct distance
      });
    }
print("index 0 "+distances[0].toString()); //prints nothing

就像在for循环之外我丢失了distances列表中的所有值

【问题讨论】:

    标签: asynchronous flutter dart future


    【解决方案1】:

    还可以:

    var dist =  await calculateDistance(list[i]);
    

    它会等待 Future 返回 int 值。

    另一种解决方案是:

    calculateDistance(list[i]).then((dist) {list[i].distance=dist;})
    

    Future 完成后,运行函数。

    【讨论】:

    • 我已经尝试过您的解决方案,但在 for-cycle 之外,它就像列表为空。print("index 0 "+distances[0].toString()); 什么都不打印
    【解决方案2】:
    import 'package:geolocator/geolocator.dart';
    import 'rack.dart';
    import 'dart:async';
    class RackList {
    
      List<Rack> _racks;
      RackList(this._racks);
      get racks => _racks;
    
      factory RackList.fromJson(Map<String,dynamic> json){
        var list = json['racks'] as List;
    
         calculateDistance(var element) async{
          var startLatitude=45.532;
          var startLongitude=9.12246;
          var endLatitude=element.latitude;
          var endLongitude=element.longitude;
          var dist = await Geolocator().distanceBetween(
              startLatitude,
              startLongitude,
              endLatitude,
              endLongitude);
          int distance=(dist/1000).round();
          return distance;
        }
    
        list = list.map((i) => Rack.fromJson(i)).toList();
    
          for (int i = 0; i < list.length; i++) {
            calculateDistance(list[i]).then((dist){
    
            print("${dist}"); //prints Instance of 'Future<int>'
            list[i].distance=dist; //crash
          });
        }
    
        list.sort((a, b) => a.distance.compareTo(b.distance));
        return RackList(list);
      }
    } 
    

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 2020-05-12
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 2016-06-10
      • 1970-01-01
      • 2011-12-21
      相关资源
      最近更新 更多