【问题标题】:Flutter Geo location error in Address and App Data file地址和应用程序数据文件中的颤振地理位置错误
【发布时间】:2021-07-06 18:31:00
【问题描述】:

使用 Flutter 2.2.2 Android 工作室 - 4.2.1

这是我的 appData.dart 文件代码 班级地址有错误。保存位置有问题。

    import 'package:flutter/cupertino.dart';
    import 'package:riderapp/Models/address.dart';
    
    class AppData extends ChangeNotifier
    {
       Address pickUpLocation;
    
      void updatePickUpLocationAddress(Address pickUpAddress)
      {
        pickUpLocation = pickUpAddress;
        notifyListeners();
      }
    }

这是我的 address.dart 文件 地址行有错误

    class Address
    {
      String placeFormattedAddress;
      String placeName;
      String placeId;
      double latitude;
      double longitude;
    
    
      Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
    }

【问题讨论】:

  • 确切的错误是什么?
  • 错误:必须初始化不可为空的实例字段“pickUpLocation”。 (not_initialized_non_nullable_instance_field at [riderapp] lib/DataHandler/appData.dart:6)
  • 错误:参数 'placeId' 由于其类型,不能有值'null',但隐含的默认值是'null'。 ([riderapp] lib/Models/address.dart:10 处缺少_default_value_for_parameter)
  • 错误:参数 'placeName' 的值不能为 'null',因为它的类型,但隐含的默认值是 'null'。 ([riderapp] lib/Models/address.dart:10 处缺少_default_value_for_parameter)
  • 错误:参数“纬度”由于其类型不能有值“空”,但隐含的默认值是“空”。 ([riderapp] lib/Models/address.dart:10 处缺少_default_value_for_parameter)

标签: android flutter dart geolocation


【解决方案1】:

根据我的经验,这来自 dart 2.0 中引入的新的 null 安全功能。我建议您查看official dev docs 了解更多信息。

你有两个选择。

1:使用 ? 使它们都可以为空?

因此,您的代码将如下所示(仅 address.dart)

    class Address
    {
      String? placeFormattedAddress;
      String? placeName;
      String? placeId;
      double? latitude;
      double? longitude;
    
    
      Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
    }

但是,这可能会在未来导致更多关于空安全检查的问题。

2:用默认值初始化它们,防止初始化为空。

    class Address
    {
      String placeFormattedAddress = '';
      String placeName = '';
      String placeId = '';
      double latitude = 0.0;
      double longitude = 0.0;
    
    
      Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
    }

【讨论】:

  • 这个错误是怎么回事?Address pickUpLocation;
  • 只需添加地址pickUpLocation = Address();地址将是这里的默认构造函数。如果答案有帮助,请不要忘记点击问题附近的绿色复选标记并投票来接受
  • 使用此代码后 mainscreen.dart 中的错误 Provider.of(context).pickUpLocation != null ? Provider.of(context).pickUpLocation.placeName : "Add Home"
  • 这似乎是一个完全不同的问题,它谈论您使用的提供程序包。一方面,我不知道你的主 screen.dart 文件是什么样子的。
  • 这段代码不能正常工作后 ​​class Address { String placeFormattedAddress = '';字符串地点名称 = '';字符串 placeId = '';双纬度 = 0.0;双经度 = 0.0;地址({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude}); }
【解决方案2】:

只需添加

Address pickUpLocation = Address(placeFormattedAdress:'',placeName:'',placeId:'',latitude:0.0,longitude:0.0);

这实际上解决了我的问题,代码运行完美。

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 2020-11-02
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多