简体   繁体   中英

Flutter Autocomplete google places form

I want to implement an Autocomplete google place in my Text form field. However I have below which I cannont understand. Google Places is not recognized and Late is marked as text. If someone can help me it will be great. Thank you in advance. I actually have this issue :

First error : The method 'GoogleMapPlaces' isn't defined for the type '_RegisterScreen2State'. Second error : Unexpected text 'late'.

You can see both errors marked in red in my image added below

**I am using last flutter version, flutter_google_places: ^0.3.0 and 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);
    }
  }
}

ISSUE MARKED IN RED

First error is a typo, you have written GoogleMapPlaces instead of GoogleMapsPlaces .

Then, you can do one of two things, depending on whether you have migrated to dart 2.13 or not.

Change your late GoogleMapsPlaces _places = .... line to one of the following:

If you have null-safety

late GoogleMapsPlaces _places;

If you don't have null-safety

GoogleMapsPlaces _places;

This should fix both your errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM