You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter用户端点击餐厅按钮报FormatException: Invalid double null错误求助

问题根因

报错FormatException: Invalid double null是因为代码调用double.parse()方法时,传入的字符串是"null",转换失败触发异常。商铺入口正常、餐厅入口报错,说明你的Firebase数据库中至少有一条餐厅数据存在空值字段,触发了转换逻辑异常:

  • 餐厅数据的lat/long(经纬度)字段为空
  • 餐厅的菜单/活动商品的productPrice(商品价格)字段为空
    空值调用toString()方法会返回字符串"null",后续转double时直接触发报错。

修复方案

1. 数据转换阶段加空值兜底(必改)

修改fetchAndSetNames方法中餐厅数据的赋值逻辑,对所有要转double的字段做空值兼容:

处理经纬度空值

把原来餐厅数据的经纬度赋值:

namesLat: resData['lat'].toString(),
namesLong: resData['long'].toString(),

替换为:

namesLat: (resData['lat'] ?? 0.0).toString(),
namesLong: (resData['long'] ?? 0.0).toString(),

处理商品价格空值

把原来餐厅菜单、活动商品的价格赋值:

price: double.parse(e["productPrice"].toString()),

替换为:

price: double.tryParse(e["productPrice"]?.toString() ?? "") ?? 0.0,

2. 列表渲染阶段过滤无效数据(可选)

如果不需要展示经纬度为0的无效测试餐厅,可以修改restaurantNames方法,跳过无效数据避免无意义的距离计算:

List<NamesItem> restaurantNames(String userLat, String userLong) {
  List<NamesItem> nearestRestaurant = [];
  _restaurantNames.forEach((element) {
    // 过滤经纬度无效的餐厅
    if (element.namesLat == null || element.namesLong == null 
        || element.namesLat == "0.0" || element.namesLong == "0.0") {
      return;
    }
    var distanceBetween = calculateDistance(
      double.parse(userLat),
      double.parse(userLong),
      double.parse(element.namesLat),
      double.parse(element.namesLong),
    );
    if (distanceBetween < 10) {
      nearestRestaurant.add(element);
    }
  });
  return [...nearestRestaurant];
}

内容的提问来源于stack exchange,提问作者Nova World

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 17:36:05