简体   繁体   中英

Convert Future<int> to int in Flutter

My goal is to sort a list of racks compared to the geo position of the user. Here's my code:

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);
  }
}

The problem is in the for cycle, the variable dist is a Future<int> type and cannot be assigned to list[i].distance . How can I convert that value to a normal int?

I've tried the solution of @Nuts but:

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

It's like outside the for-cycle I lost all the values inside the distances list

Could also:

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

It's gonna wait for the Future to return int value.

Another solution would be:

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

When Future is complete, then run function.

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);
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM