Home > Blockchain >  Flutter - Instance of 'Future<double>' instead number
Flutter - Instance of 'Future<double>' instead number

Time:09-06

I'm on Flutter project and from the code below I retrieved the distance from the user's position and a fixed place.

But when I compile the code, the app shows Instance of 'Future<double>' instead of distance but with print my console shows the distance:

class ProductHorizontalListItem extends StatelessWidget {
  const ProductHorizontalListItem({
    Key? key,
    required this.product,
    required this.coreTagKey,
    this.onTap,
  }) : super(key: key);

  final Product product;
  final Function? onTap;
  final String coreTagKey;

  @override
  Widget build(BuildContext context) {
    // print('***Tag*** $coreTagKey${PsConst.HERO_TAG__IMAGE}');
    final PsValueHolder valueHolder =
    Provider.of<PsValueHolder>(context, listen: false);

Future<double> getCurrentLocation() async {
      Position position = await Geolocator.getCurrentPosition();
      double lat = position.latitude;
      double long = position.longitude;
      final double distanceInMeters = Geolocator.distanceBetween(
        double.parse(position.latitude.toString()),
        double.parse(position.longitude.toString()),
        double.parse(product.itemLocation!.lat.toString()),
        double.parse(product.itemLocation!.lng.toString()),
      );
        print(distanceInMeters);
        return await distanceInMeters;

    }

    final Future<void> position = getCurrentLocation();

    return InkWell(
        onTap: onTap as void Function()?,
        child: Container(
            margin: const EdgeInsets.only(
                left: PsDimens.space4, right: PsDimens.space4,
                bottom: PsDimens.space12),
            child: Text(
                '${position}' '${product.itemLocation!.name}',
                textAlign: TextAlign.start,
                style: Theme.of(context).textTheme.caption!.copyWith(
                    color: PsColors.textColor3
                )))

        );
  }
}

I cannot understand what is wrong.

Thank you.

CodePudding user response:

The are a couple of things.

  1. distanceInMeters is not a Future, so instead of using the await keyword before returning, you should return the double value from getCurrentLocation like this:
return Future.value(distanceInMeters);
  1. This code: final Future<void> position = getCurrentLocation(); is problematic, because getCurrentLocation() has a type of Future<double>. Normally you should use it like below, but this is a build function, where you are not allowed to use await, you can't mark the build function as async.
final position = await getCurrentLocation();
  1. To solve your issue you could use a FutureBuilder, which will wait for the result of getCurrentLocation() and build the widget once the future is completed. Please refer to the documentation for details (especially on how to create the future properly), but basically something like this:
return FutureBuilder<double>(
  future: getCurrentLocation(),
  builder: (BuildContext context, AsyncSnapshot<double> snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return CircularProgressIndicator();
    }
    return InkWell(...); // use `snapshot.data` to get the double value 
});
  • Related