我想在我的文本表单字段实现自动完成谷歌的地方。然而,我有以下我无法理解。Google Places无法识别,Late被标记为文本。如果有人能帮我,那就太好了。提前谢谢你。我实际上有这个问题:
第一个错误:'GoogleMapPlaces'方法没有为'_RegisterScreen2State'类型定义。第二个错误:意外文本'late'.
你可以看到在我的图像中添加的红色标记的两个错误
**我使用的是最后一个flutter版本,flutter_google_places: ^0.3.0和geocoder: ^0.2.1
enum MobileVerificationState {
SHOW_MOBILE_FORM_STATE, // The user provides the phone number
SHOW_OTP_FORM_STATE // The user has to enter the code he received
}
class RegisterScreen2 extends StatefulWidget {
final UsersInformations user;
const RegisterScreen2(this.user);
@override
_RegisterScreen2State createState() => _RegisterScreen2State();
}
class _RegisterScreen2State extends State<RegisterScreen2> {
@override
void initState(){
super.initState();
_places = GoogleMapPlaces(apiKey: kGoogleApiKey);
}
final kGoogleApiKey = "MY IPA KEY";
late GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
.....
getMobileFormWidget(context, user) {
return Scaffold(
backgroundColor: Colors.white,
body: Form(
child: SingleChildScrollView(
child: Column(
children: [
Stack(
children: [
Container(
height: 100,
decoration: BoxDecoration(
color: Colors.green.shade50,
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(30))),
),
Padding(
padding: const EdgeInsets.only(top: 70),
child: Stack(
// ignore: prefer_const_literals_to_create_immutables
children: [
Center(
child: CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
backgroundImage: image != null
? FileImage(image)
: const NetworkImage(
"https://cdn-icons-png.flaticon.com/512/1177/1177568.png"),
),
),
Positioned(
bottom: -5,
right: 145,
child: InkWell(
onTap: () => getImage(),
child: Icon(Icons.camera_alt_rounded)))
],
),
),
],
),
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Padding(
padding: EdgeInsets.only(left: 15, right: 15, top: 5),
child: TextFormField(
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey.shade300)),
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.deepPurple.shade900)),
focusedErrorBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red.shade900)),
errorBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red.shade900)),
hintText: "Adresse",
),
onChanged: (val) async {
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
mode: Mode.overlay, // Mode.fullscreen
language: "fr",
components: [
new Component(Component.country, "fr"),
]);
displayPrediction(p);
user.setAdress = val;
},
),
),
),
…
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);
print(lat);
print(lng);
}
}
}
红色标记的问题
第一个错误是打字错误,您将GoogleMapPlaces
写成了GoogleMapsPlaces
然后,您可以做两件事中的一件,这取决于您是否已经迁移到dart 2.13。
将您的late GoogleMapsPlaces _places = ....
行更改为以下内容之一:
如果你有null-safety
late GoogleMapsPlaces _places;
如果你没有null-safety
GoogleMapsPlaces _places;
这应该可以修复你的两个错误。