Flutter i';我试图使用地理编码api获取地名,但它没有';它说placename是空的
我为geolocator创建了单独的文件,称为placename和address属性的帮助方法和地址类,并将它们包含在搜索页面中。这是给定的错误 =======小部件库捕获的异常======================================================= 对null调用了getter“placename”。 收件人:空 尝试呼叫:placename 这是搜索页面文件Flutter i';我试图使用地理编码api获取地名,但它没有';它说placename是空的,flutter,google-maps,google-geocoding-api,geopositioning,Flutter,Google Maps,Google Geocoding Api,Geopositioning,我为geolocator创建了单独的文件,称为placename和address属性的帮助方法和地址类,并将它们包含在搜索页面中。这是给定的错误 =======小部件库捕获的异常======================================================= 对null调用了getter“placename”。 收件人:空 尝试呼叫:placename 这是搜索页面文件 import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:reviver_app/dataproviders/appdata.dart';
import 'package:reviver_app/globalvariables.dart';
import 'package:reviver_app/helpers/requesthelper.dart';
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
var pickupController = TextEditingController();
var destinationController = TextEditingController();
var focusDestination = FocusNode();
bool focused = false;
void setFocus() {
if (!focused) {
FocusScope.of(context).requestFocus(focusDestination);
focused = true;
}
}
void searchPlace(String placeName) async {
if (placeName.length >1){
String url = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$placeName+Amphitheatre&key=$mapKey&sessiontoken=123456789';
var response = await RequestHelper.getRequest(url);
if (response == 'failed'){
return;
}
print(response);
}
}
@override
Widget build(BuildContext context) {
setFocus();
String address = Provider.of<AppData>(context).pickupaddress.placename ?? ""; this is the error which is null
pickupController.text = address;
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: 210,
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.redAccent,
blurRadius: 5.0,
spreadRadius: 0.5,
offset: Offset(
0.7,
0.7,
),
),
]
),
child: Padding(
padding: EdgeInsets.only(left: 24, top: 48, right:24, bottom: 20 ),
child: Column(
children: <Widget> [
SizedBox(height: 5),
Stack(
children: <Widget>[
GestureDetector(
onTap: (){
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back, color: Colors.redAccent,)
),
Center(
child: Text('Set Booking', style:
TextStyle(fontSize: 20, fontFamily: 'Brand-Bold'),
),
),
],
),
SizedBox(height: 18,),
Row(
children: <Widget> [
Image.asset('images/pickicon.png', height:16, width: 16 ,),
SizedBox(width: 18,),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(4),
),
child: Padding(
padding: EdgeInsets.all(2.0),
child: TextField(
controller: pickupController,
decoration: InputDecoration(
hintText: 'Your Location',
fillColor: Colors.redAccent,
filled: true,
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(left: 10, top: 0, bottom: 0)
),
),
),
),
),
],
),
SizedBox(height: 10,),
Row(
children: <Widget> [
Image.asset('images/desticon.png', height:16, width: 16 ,),
SizedBox(width: 18,),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(4),
),
child: Padding(
padding: EdgeInsets.all(2.0),
child: TextField(
//destination which will be removed
onChanged: (value){
searchPlace(value);
},
focusNode: focusDestination ,
controller: destinationController,
decoration: InputDecoration(
hintText: 'this will be removed ',
fillColor: Colors.redAccent,
filled: true,
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(left: 10, top: 0, bottom: 0)
),
),
),
),
),
],
),
],
),
),
)
],
),
);
}
}
```
this is where I included the geolocotor URL
这是我在其中添加地名的address类
class Address {
String placename;
double latitude;
double longitude;
String placeId;
String placeformattedaddress;
Address({
this.placeId,
this.latitude,
this.placename,
this.longitude,
this.placeformattedaddress,
});
}
import 'package:flutter/cupertino.dart';
import 'package:reviver_app/datamodels/address.dart';
class AppData extends ChangeNotifier{
Address pickupaddress;
void updatePickupAddress(Address pickup){
pickupaddress = pickup;
notifyListeners();
}
}
class Address {
String placename;
double latitude;
double longitude;
String placeId;
String placeformattedaddress;
Address({
this.placeId,
this.latitude,
this.placename,
this.longitude,
this.placeformattedaddress,
});
}