首页 > 解决方案 > Flutter 自动完成谷歌地方表格

问题描述

我想在我的文本表单字段中实现一个自动完成的谷歌位置。但是我有以下我无法理解的内容。无法识别 Google 地方信息并且迟到被标记为文本。如果有人可以帮助我,那就太好了。先感谢您。我实际上有这个问题:

第一个错误:没有为类型“_RegisterScreen2State”定义方法“GoogleMapPlaces”。第二个错误:意外的文本“迟到”。

您可以在下面添加的我的图片中看到两个标记为红色的错误

**我正在使用最后一个颤振版本,flutter_google_places:^0.3.0 和地理编码器:^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);
    }
  }
}

问题标记为红色

标签: iosflutterdartgoogleplacesautocomplete

解决方案


第一个错误是一个错字,你写GoogleMapPlaces的不是GoogleMapsPlaces.

然后,您可以执行以下两项操作之一,具体取决于您是否已迁移到 dart 2.13。

late GoogleMapsPlaces _places = ....将您的行更改为以下之一:

如果你有零安全

late GoogleMapsPlaces _places;

如果您没有零安全性

GoogleMapsPlaces _places;

这应该可以解决您的两个错误。


推荐阅读