Google Map in Flutter App is not showing up

淺唱寂寞╮ 提交于 2021-01-29 08:36:38

问题


I have a problem in intergrating a simple GoogleMap in my Flutter App. I correctly inserted the API Key in my Manifest file and inserted the library in the app. But the emulator is just showing a blank page. I am doing nothing special until now; just trying to create a GoogleMap.

This is the Code i am using in the App:

return Stack(
 children: <Widget>[
   GoogleMap(initialCameraPosition: CameraPosition(target:
   LatLng(-33.870840,151.206286),
   zoom: 12)
   )
 ], );

What the emulator is showing:

The first lines in the console(which i think are of special meaning):

E/flutter ( 5736): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/google_maps

I tried several workarounds but only ended up with more errors. Looking Forward to your answers!


回答1:


I tried to add Google Map in a fresh project and was able to see it on emulator successfully. I followed this article step by step and used your code snippet to show the map and it worked.

Complete code used:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Completer<GoogleMapController> _controller = Completer();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Maps Sample App'),
          backgroundColor: Colors.green[700],
        ),
        body: Stack(
      children: <Widget>[
      GoogleMap(initialCameraPosition: CameraPosition(target:
      LatLng(-33.870840,151.206286),
        zoom: 12)
    )
    ],
        )
      ),
    );
  }
}

Couple of points to note:

  1. Double check if the api is enabled (Maps SDK for Android) when you generated key for it.
  2. Do flutter clean once you cross-check to confirm you've latest dependencies.

Hope this helps.



来源:https://stackoverflow.com/questions/59383205/google-map-in-flutter-app-is-not-showing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!