如何使用flutter_google_places_autocomplete.dart解决错误?

时间:2019-07-02 19:39:34

标签: google-maps flutter dart googleplacesautocomplete

我使用Google地图制作了一个应用程序,您可以在其中找到一个地点并在地图上建立通往该地点的路径。我想根据用户键入的内容在其下方显示地点和地址。显示结果后,我需要获取其经度和纬度以在地图上进行标记。

我尝试使用flutter_google_places: 0.2.3,但未定义函数(GoogleMapsPlaces,Prediction)。接下来,我使用了flutter_google_places_autocomplete: 0.1.3,一切都很好。不幸的是,当我尝试运行项目时,出现错误:

  

编译器消息:   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:   错误:位置参数过多:允许0,但找到1。尝试   删除多余的位置参数。       _places =新的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:   上下文:找到了该候选者,但参数不匹配。
  GoogleMapsPlaces({^编译失败   C:\ Users \ admin \ AndroidStudioProjects \ advertise_me \ lib \ main.dart

     

失败:构建失败,并出现异常。

     
      
  • 其中:脚本'C:\ Users \ admin \ flutter \ packages \ flutter_tools \ gradle \ flutter.gradle'   行:647

  •   
  • 出了什么问题:任务':app:compileflutterBuildDebugandroid-arm64'的执行失败。

         
        

    处理'命令'C:\ Users \ admin \ flutter \ bin \ flutter.bat'完成,退出值非零1

      
  •   
  • 尝试:使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。使用--scan运行以获取完整的见解。

  •   
  • https://help.gradle.org

  • 获得更多帮助   
     

在9秒钟内构建失败,但出现错误:Gradle任务assembleDebug   失败,退出代码为1

我的pubspes.yaml:

  

依赖性:颤动:       sdk:颤抖

     

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地理编码器:0.1.2
  google_maps_webservice:0.0.14

我的代码:

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

如何解决此错误? 非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

flutter_google_places_autocomplete已过时,应改为使用flutter_google_places。查看文档:{​​{3}}

使用flutter_google_places后,请使用

Prediction p = await PlacesAutoComplete.show()而不是showGooglePlacesAutoComplete()

========更新的答案=====

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

答案 1 :(得分:0)

与导入'package:google_maps_webservice/places.dart';一起使用导入'package:flutter_google_places/flutter_google_places.dart';