Vlados
Vlados

Reputation: 151

How to fix error with flutter_google_places_autocomplete.dart?

I make an application with Google maps in which you can find a place and build a path to it on the map. I want to show places and addresses below it according to what the user types.After showing the results, I need to get its latitude and longitude to mark on the map.

I tried to use a flutter_google_places: 0.2.3 but functions(GoogleMapsPlaces, Prediction) were not defined. Next i used flutter_google_places_autocomplete: 0.1.3 and everything was fine. Unfortunately when i tride to run the project I got an error:

Compiler message: file:///C:/Users/admin/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/flutter_google_places_autocomplete-0.1.3/lib/src/flutter_google_places_autocomplete.dart:337:35: Error: Too many positional arguments: 0 allowed, but 1 found. Try removing the extra positional arguments. _places = new GoogleMapsPlaces(widget.apiKey); ^ file:///C:/Users/admin/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/google_maps_webservice-0.0.14/lib/src/places.dart:22:3: Context: Found this candidate, but the arguments don't match.
GoogleMapsPlaces({ ^ Compiler failed on C:\Users\admin\AndroidStudioProjects\advertise_me\lib\main.dart

FAILURE: Build failed with an exception.

  • Where: Script 'C:\Users\admin\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 647

  • What went wrong: Execution failed for task ':app:compileflutterBuildDebugandroid-arm64'.

    Process 'command 'C:\Users\admin\flutter\bin\flutter.bat'' finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 9s Finished with error: Gradle task assembleDebug failed with exit code 1

My pubspes.yaml:

dependencies: flutter: sdk: flutter

cupertino_icons: ^0.1.2 url_launcher: ^4.2.0+1
google_maps_flutter: bottom_sheet_stateful: ^0.1.1
flutter_google_places_autocomplete: 0.1.3 geocoder: 0.1.2
google_maps_webservice: 0.0.14

My code:

import 'package:flutter/material.dart';
import 'package:advertise_me/login_screen.dart';
import 'dart:async';
import 'package:google_maps_webservice/geocoding.dart';
//import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:flutter_google_places_autocomplete/flutter_google_places_autocomplete.dart';
import 'package:geocoder/geocoder.dart';


void main() => runApp(MyApp());

const kGoogleApiKey = "My key";


GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: demo(),
      ),
    );
  }
}

class demo extends StatefulWidget {
  @override
  demoState createState() => new demoState();
}

class demoState extends State<demo> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
        body: Container(
            alignment: Alignment.center,
            child: RaisedButton(
              onPressed: () async {
                Prediction p = await showGooglePlacesAutocomplete(
                    context: context, apiKey: kGoogleApiKey);
                displayPrediction(p);
              },
              child: Text('Find address'),

            )
        )
    );
  }

  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);
    }
  }
}

How do i fix this error? Any help is much appreciated.

Upvotes: 1

Views: 9187

Answers (3)

Kashif Ahmad
Kashif Ahmad

Reputation: 263

Use complete list of function parameters like:

Prediction p = await PlacesAutocomplete.show( offset: 0, radius: 1000, types: [], strictbounds: false,
region: "ar", context: context, apiKey: googleAPIKey, mode: Mode.overlay, language: "es", components: [Component(Component.country, "ar")] );

Upvotes: 1

user321553125
user321553125

Reputation: 3216

use import 'package:google_maps_webservice/places.dart'; along with, import 'package:flutter_google_places/flutter_google_places.dart';

Upvotes: 0

Darshan
Darshan

Reputation: 11634

flutter_google_places_autocomplete is deprecated and you should use flutter_google_places instead. See documentation : https://pub.dev/packages/flutter_google_places_autocomplete

And once you use flutter_google_places, use

Prediction p = await PlacesAutoComplete.show() instead of showGooglePlacesAutoComplete()

======= updated answer =====

import 'package:flutter/material.dart';
//import 'package:advertise_me/login_screen.dart';
import 'dart:async';
import 'package:flutter_google_places/flutter_google_places.dart';
import 'package:geocoder/geocoder.dart';
import 'package:google_maps_webservice/places.dart';


void main() => runApp(MyApp());

const kGoogleApiKey = "My key";


GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: demo(),
      ),
    );
  }
}

class demo extends StatefulWidget {
  @override
  demoState createState() => new demoState();
}

class demoState extends State<demo> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
        body: Container(
            alignment: Alignment.center,
            child: RaisedButton(
              onPressed: () async {
                Prediction p = await PlacesAutocomplete.show(
                    context: context, apiKey: kGoogleApiKey);
                displayPrediction(p);
              },
              child: Text('Find address'),

            )
        )
    );
  }

  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);
    }
  }
}

Upvotes: 3

Related Questions