0

I have been trying to get Flutter Google maps to load for a couple of weeks. google_maps_flutter.dart. I've tried just about everything I can think of. I am new to Flutter so I'm sure it's some rookie mistake but I can't figure it out. I have the package loaded and the dependency set in the yaml file. I have updated the API's, actually twice to make sure they are correct. Both the Android and iOS SDK are Enabled on the Google Cloud. I put in some 'print tests' and they go up to and beyond the GoogleMaps call but nothing happens. Not so much as a blip in the screen other than when I click the Route button. I'm including the code below hoping someone can correct it for me, please. It prints all three test prints but nothing happens with the Google Maps at all.

import 'dart:async';

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

const _url = 'https://store-x36sk.mybigcommerce.com/ice-cream-flavors/';
const _menuurl = 'https://store-x36sk.mybigcommerce.com/menu/';

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

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

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

  static const LatLng _center =
      const LatLng(37.42796133580664, -122.085749655962);

  _launchURL() async {
    return await canLaunch(_url)
        ? await launch(_url)
        : throw 'Could not launch $_url';
  }

  _menulaunchURL() async {
    return await canLaunch(_menuurl)
        ? await launch(_menuurl)
        : throw 'Could not launch $_menuurl';
  }

  void _onMapCreated(GoogleMapController controller) {
    _controller.complete(controller);
  }

  _maplaunch() async {
    print('1st test');
    // return
    print('2nd test');
    // MaterialApp(
    // home: Scaffold(
    //   appBar: AppBar(
    //     title: Text('On Your Way to Lake City'),
    //     backgroundColor: Colors.purple.shade100,
    //   ),
    //   body:
    await GoogleMap(
      onMapCreated: _onMapCreated,
      // mapType: MapType.hybrid,
      initialCameraPosition: CameraPosition(
        target: _center,
        zoom: 11.0,
      ),

      // ),
      // ),
    );
    print('3rd test');
  }

  @override
  Widget build(BuildContext context) {
    Widget titleSection = Container(
      padding: const EdgeInsets.all(32),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  padding: const EdgeInsets.only(bottom: 2),
                  child: Text(
                    'Lake City Creamery & Coffee',
                    style: TextStyle(
                      color: Colors.purple,
                      fontWeight: FontWeight.bold,
                      fontSize: 20.0,
                    ),
                  ),
                ),
                Text(
                  '5465 St Rt 29, Celina, OH',
                  style: TextStyle(
                    color: Colors.purple,
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                //   FavoriteWidget(),
              ],
            ),
          ),
        ],
      ),
    );

    Color color = Theme.of(context).primaryColor;

    Widget buttonSection = Container(
      padding: const EdgeInsets.only(left: 5, top: 5, right: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          MaterialButton(
            onPressed: _launchURL,
            child: _buildButtonColumn(Colors.purple, Icons.icecream, 'Flavors'),
            padding: EdgeInsets.all(16),
            shape: CircleBorder(),
            color: Colors.pink.shade50,
          ),
          MaterialButton(
            onPressed: _menulaunchURL,
            child: _buildButtonColumn(Colors.purple, Icons.restaurant, 'Menu'),
            padding: EdgeInsets.all(16),
            shape: CircleBorder(),
            color: Colors.pink.shade50,
          ),
          MaterialButton(
            onPressed: () {},
            // TO DO onPressed
            child: _buildButtonColumn(Colors.purple, Icons.share, 'Share'),
            padding: EdgeInsets.all(16),
            shape: CircleBorder(),
            color: Colors.pink.shade50,
          ),
          MaterialButton(
            onPressed: _maplaunch,
            child: _buildButtonColumn(Colors.purple, Icons.directions, 'Route'),
            padding: EdgeInsets.all(16),
            shape: CircleBorder(),
            color: Colors.pink.shade50,
          ),
        ],
      ),
    );
SherriB
  • 23
  • 7

2 Answers2

0

GoogleMap is a widget so you should put on the render function. In this case, you want to show the map when the user taps the button.

_maplaunch() async {
  Navigator.of(context).push(MaterialPageRoute(builder: (_) => RoutePage()));
}

And add a new Page

class RoutePage extends StatelessWidget {
  static const LatLng _center =
      const LatLng(37.42796133580664, -122.085749655962);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GoogleMap(
        // mapType: MapType.hybrid,
        initialCameraPosition: CameraPosition(
          target: _center,
          zoom: 11.0,
        ),
      ),
    );
  }
}
Luis A. Chaglla
  • 580
  • 4
  • 8
  • Thank you Luis! At least now I am getting an error. I don't know how to fix this. I added the first piece to the main.dart in the stateful widget area. I added the second portion to a new .dart file. Now I am getting the following errors: The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget. #0 Navigator.of. (package:flutter/src/widgets/navigator.dart:2711:9) – SherriB Apr 29 '21 at 19:13
  • You should wrap your app with MaterialApp try this: 'void main() => runApp(MaterialApp(home: MyApp()));' – Luis A. Chaglla Apr 29 '21 at 19:17
  • I wrapped the Navigator in a build context and now the errors are gone but I am back to the original problem of nothing happening when I click the Route button. I'll try what you mention above. I replied to this before I saw your reply. – SherriB Apr 29 '21 at 21:37
  • Maybe the button shows an overflow error? Add a `print('tapped');` in _maplaunch and see if its printed – Luis A. Chaglla Apr 30 '21 at 09:47
0

Welcome to Stackoverflow. You mentioned that you have done everything, except adding this to you AndroidManifest.xml file, also same thing for info.plist on iOS.

This is how it'll look for Android.

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="xxxxxxxxxxx-xxxxxxxxxxxxxx"
/>
Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49