Android 如何从PlacesaToComplete中的places中选择值并将其存储在TextFormField中
我想用PlacesAutocomplete获取位置,并将其存储在我的TextFormField中。Android 如何从PlacesaToComplete中的places中选择值并将其存储在TextFormField中,android,flutter,dart,google-places-api,google-places,Android,Flutter,Dart,Google Places Api,Google Places,我想用PlacesAutocomplete获取位置,并将其存储在我的TextFormField中。 我正在从TextFormFeild() 以下是显示预测()的displayPrediction(): 我想在点击我的TextFormField后获得位置,并在同一TextFormField中显示相同的位置 我在选择了一个地方后出现了这个错误-这里是演示 final TextEditingController _controller = TextEditingController(); Text
我正在从
TextFormFeild()
以下是显示预测()的displayPrediction()
:
我想在点击我的TextFormField后获得位置,并在同一TextFormField中显示相同的位置
我在选择了一个地方后出现了这个错误-这里是演示
final TextEditingController _controller = TextEditingController();
TextFormField( controller: _controller ,
decoration: textInputDecoration.copyWith(hintText: 'From'),
onTap: () async {
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
mode: Mode.overlay,
//language: "en",
//components: [Component (Component.country, "en")],
);
displayPrediction(p);
},
),
看起来您正在使用geocoder库
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
var placeId = p.placeId;
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
var address = await Geocoder.local.findAddressesFromQuery(p.description);
setState(() {
_controller.text = address.first.featureName;
});
print(lat);
print(lng);
print(address);
}
}
未来显示预测(预测p)异步{
如果(p!=null){
位置细节响应细节=
等待地点。getDetailsByPlaceId(p.placeId);
var placeId=p.placeId;
双lat=detail.result.geometry.location.lat;
双液化天然气=详图.result.geometry.location.lng;
var address=await Geocoder.local.findAddressesFromQuery(p.description);
设置状态(){
_controller.text=address.first.featureName;
});
打印(lat);
印刷品(液化天然气);
印刷品(地址);
}
}
希望有帮助这里是演示
final TextEditingController _controller = TextEditingController();
TextFormField( controller: _controller ,
decoration: textInputDecoration.copyWith(hintText: 'From'),
onTap: () async {
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
mode: Mode.overlay,
//language: "en",
//components: [Component (Component.country, "en")],
);
displayPrediction(p);
},
),
看起来您正在使用geocoder库
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
var placeId = p.placeId;
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
var address = await Geocoder.local.findAddressesFromQuery(p.description);
setState(() {
_controller.text = address.first.featureName;
});
print(lat);
print(lng);
print(address);
}
}
未来显示预测(预测p)异步{
如果(p!=null){
位置细节响应细节=
等待地点。getDetailsByPlaceId(p.placeId);
var placeId=p.placeId;
双lat=detail.result.geometry.location.lat;
双液化天然气=详图.result.geometry.location.lng;
var address=await Geocoder.local.findAddressesFromQuery(p.description);
设置状态(){
_controller.text=address.first.featureName;
});
打印(lat);
印刷品(液化天然气);
印刷品(地址);
}
}
希望它有帮助谢谢它对“From”很好,但是它在“To”上显示相同的位置,如果我选择“To”TextFormFeild来更改位置,它会显示一个错误:_TypeError(type“List”不是type“PlaceDetails”的子类型)使用不同的
final TextEditingController _controller=TextEditingController()代码>对于“TO”字段,我创建了不同的displayPrediction和_controller,但即使在“TO”显示错误之后,这也是类型转换问题,请重新检查变量DataTypeWait _places.getDetailsByPlaceId(p.placeId);谢谢,它对“From”很好,但是它在“To”上显示相同的位置,如果我选择“To”TextFormFeild来更改位置,它会显示一个错误:_TypeError(type“List”不是type“PlaceDetails”的子类型)使用不同的final TextEditingController _controller=TextEditingController()代码>对于“TO”字段,我创建了不同的displayPrediction和_controller,但即使在“TO”显示错误之后,这也是类型转换问题,请重新检查变量DataTypeWait _places.getDetailsByPlaceId(p.placeId);
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
var placeId = p.placeId;
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
var address = await Geocoder.local.findAddressesFromQuery(p.description);
setState(() {
_controller.text = address.first.featureName;
});
print(lat);
print(lng);
print(address);
}
}