使用过的Flutter 2.2.2安卓工作室-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();
}
}
这是我的地址。艺术文件地址行有错误
class Address
{
String placeFormattedAddress;
String placeName;
String placeId;
double latitude;
double longitude;
Address({this.placeFormattedAddress, this.placeName, this.placeId, this.latitude, this.longitude});
}
根据我的经验,这来自于dart 2.0中引入的新的null安全功能。我建议查看官方的开发文档以获取更多信息。
你有两个选择。
1:使用
因此,您的代码将看起来像(onlyaddress.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 = Address(placeFormattedAdress:'',placeName:'',placeId:'',latitude:0.0,longitude:0.0);
这实际上解决了我的问题,代码运行得很完美。