简体   繁体   English

Flutter - FutureBuilder 在 Android 上运行良好,但在 iOS 上运行不佳

[英]Flutter - FutureBuilder works well on Android but not well on iOS

I implemented the FutureBuilder with the code below in order to get the distance from the user and the item that he wants buy but I get a weird result between Android and iOS .我用下面的代码实现了FutureBuilder以获得与用户的距离以及他想要购买的物品,但我在 Android 和 iOS 之间得到了一个奇怪的结果

On Android works well and I get the distance for each item.Android 上运行良好,我得到了每个项目的距离。 But on iOS I don't have the distance for each item and infact some item has the distance and some items get null value.但是在iOS上,我没有每个项目的距离,实际上有些项目有距离,有些项目得到 null 值。

在此处输入图像描述

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) {
    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()),
          );

            return Future.value(distanceInMeters);

        }

        return FutureBuilder<double>(
                future: getCurrentLocation(),
            builder: (BuildContext context, AsyncSnapshot<double> snapshot) {
        return InkWell(
        onTap: onTap as void Function()?,
        child: Container(
            margin: const EdgeInsets.only(
                left: PsDimens.space4, right: PsDimens.space4,
                bottom: PsDimens.space12),
            child: Text(
                '${snapshot.data}',
                textAlign: TextAlign.start,
                style: Theme.of(context).textTheme.caption!.copyWith(
                    color: PsColors.textColor3
                )))

        );});
  }
}

I also tried to handle the states of FutureBuilder in each way but nothing.我还尝试以各种方式处理 FutureBuilder 的状态,但没有。

iOS works bad, why? iOS 工作不正常,为什么?

I'm on Flutter 3.0.5 with Android Studio Chipmunk.我在 Flutter 3.0.5 和 Android Studio Chipmunk 上。

First, you should check the snapshot state before accessing its data.首先,您应该在访问其数据之前检查快照 state。 Otherwise you can get a null value, since the treatment has not been finished yet.否则你可以得到一个 null 值,因为处理还没有完成。 Check snapshot.connectionState and snapshot.hasData before accessing snapshot.data .在访问snapshot.data之前检查snapshot.connectionStatesnapshot.hasData

Then, there is no need to convert latitudes and longitudes to String, then back to double.然后,无需将纬度和经度转换为String,然后再转换回double。

Eventually, you can replace the definition of final Function? onTap;最终,您可以替换final Function? onTap; final Function? onTap; by a VoidCallback , to avoid parsing it in the Inkwell button.通过VoidCallback ,以避免在 Inkwell 按钮中解析它。

Try this out:试试这个:

@override
Widget build(BuildContext context) {
  Future<double> getCurrentLocation() async {
    Position position = await Geolocator.getCurrentPosition();
    final double distanceInMeters = Geolocator.distanceBetween(
      position.latitude,
      position.longitude,
      product.itemLocation!.latitude,
      product.itemLocation!.longitude,
    );
    return Future.value(distanceInMeters);
  }

  return FutureBuilder<double>(
    future: getCurrentLocation(),
    builder: (BuildContext context, AsyncSnapshot<double> snapshot) {
      if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
        return InkWell(
          onTap: onTap,
          child: Container(
            margin: const EdgeInsets.only(
              left: PsDimens.space4,
              right: PsDimens.space4,
              bottom: PsDimens.space12,
            ),
            child: Text(
              '${snapshot.data}',
              textAlign: TextAlign.start,
              style: Theme.of(context).textTheme.caption!.copyWith(
                    color: PsColors.textColor3,
                  ),
            ),
          ),
        );
      } else if (!snapshot.hasData) {
        // Handle error case
        return const Text('error');
      } else {
        // Display a loader or whatever to wait for the Future to complete
        return const Center(child: CircularProgressIndicator());
      }
    },
  );
}

I tried the code by replacing position and product.itemLocation by location of actual cities, and the distanceInMeters is correct.我通过实际城市的位置替换 position 和 product.itemLocation 来尝试代码,并且 distanceInMeters 是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪种编程语言可以与iOS和Android上的OpenGL ES配合使用? - What programming language works well with OpenGL ES on iOS and Android? jQuery Date时间选择器,在iOS / Android上运行良好 - jQuery Date Time picker that works well on iOS/Android Flutter 应用程序 apk 在手机上无法正常运行,但在模拟器上运行良好 - Flutter app apk does not work properly on phone but works well on emulator Android Studio-推荐的图像处理库,可与Android Studio完美配合 - Android Studio - Advisable Image Processing Library that works well with Android Studio Android 2.3.3中的NotificationCompat破解,但在Android 4.2.2中运行良好 - NotificationCompat crack in Android 2.3.3 but works well in Android 4.2.2 Android Beacon Library在一台Android 4.4设备上运行良好,但在另一台Android设备上运行良好 - Android Beacon Library works well on one Android 4.4 device, but not another 基于套接字连接的应用程序在Android 2.3中效果很好,但在Android 4.2中效果不佳 - Application based on socket connection, works well in Android 2.3 but not in android 4.2 来自android设备的Webservice调用无效,在模拟器上运行良好 - Webservice call from android device is not working,works well on emulator 应用程序在Android 5.1和6.1中运行良好,但在4.10(API 16)中崩溃 - App works well in android 5.1 and 6.1 but crashes in 4.10(API 16) Android小部件在模拟器中运行良好,但在手机上它变成了Google App小部件 - Android widget works well in emulator but on phone it turns into the Google App widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM