4

I'm developing an uber clone, but when I run the application on the web, it doesn't let me see the location on the map and I get this error, it helps!

picture of my mistake

Here you can also see when the application is executed the result

mistake Web

    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:geolocator/geolocator.dart';
    import 'package:google_maps_flutter/google_maps_flutter.dart';
    import 'package:rider_app/AllWidgets/Divider.dart';
    import 'package:rider_app/Assistants/assistantsMethods.dart';



    class MainScreen extends StatefulWidget
    {
      static const String idScreen = "mainScreen";
      @override
      _MainScreenState createState() => _MainScreenState();
    }



    class _MainScreenState extends State<MainScreen>
    {
      Completer<GoogleMapController> _controllerGoogleMaps = Completer();
      GoogleMapController newGoogleMapController;

      GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

      Position currentPosition;
      var geoLocator = Geolocator();
      double bottomPaddingOfMap = 0;

      void locatePosition() async
      {
        Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
        currentPosition = position;


        LatLng latLngPosition = LatLng(position.latitude, position.longitude);

        CameraPosition cameraPosition = new CameraPosition(target: latLngPosition, zoom: 14);
        newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));

        String address = await AssistantsMethods.searchCoordinateAddress(position);
        print("Esta es tú dirección :: " + address);
      }

      static final CameraPosition _kGooglePlex = CameraPosition(
        target: LatLng(37.42796133580664, -122.085749655962),
        zoom: 14.4746,
      );

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          key: scaffoldKey,
          appBar: AppBar(
            title: Text("Ventana Principal"),
          ),
          drawer: Container(
            color: Colors.white,
            width: 255.0,
            child: Drawer(
              child: ListView(
                children: [
                  //Drawer Header
                  Container(
                    height: 165.0,
                    child: DrawerHeader(
                      decoration: BoxDecoration(color: Colors.white),
                      child: Row(
                        children: [
                          Image.asset("images/user_icon.png", height: 65.0,width: 65.0,),
                          SizedBox(width: 16.0,),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text("Nombre del perfil", style: TextStyle(fontSize: 16.0, fontFamily: "Brand-Bold"),),
                              SizedBox(height: 6.0,), 
                              Text("Perfil Visitante"),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                  DividerWidget(),

                  SizedBox(height: 12.0,),

                  //Drawer Body Controller
                  ListTile(
                    leading: Icon(Icons.history),
                    title: Text("Historia", style: TextStyle(fontSize: 15.0),),
                  ),
                  ListTile(
                    leading: Icon(Icons.person),
                    title: Text("Perfil de visitante", style: TextStyle(fontSize: 15.0),),
                  ),
                  ListTile(
                    leading: Icon(Icons.info),
                    title: Text("Sobre", style: TextStyle(fontSize: 15.0),),
                  ),
                ],
              ),
            ),
          ),
          body: Stack(
            children: [
              GoogleMap(
                padding: EdgeInsets.only(bottom: bottomPaddingOfMap),
                mapType: MapType.normal,
                myLocationButtonEnabled: true,
                initialCameraPosition: _kGooglePlex,
                myLocationEnabled: true,
                zoomGesturesEnabled: true,
                zoomControlsEnabled: true,
                onMapCreated: (GoogleMapController controller)
                {
                  _controllerGoogleMaps.complete(controller);
                  newGoogleMapController =  controller;

                  setState(() {
                    bottomPaddingOfMap = 300.0;
                  });

                  locatePosition();

                },
              ),

              Positioned(
                top: 45.0,
                left: 22.0,
                child: GestureDetector(
                  onTap: ()
                  {
                    scaffoldKey.currentState.openDrawer();

                  },
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(22.0),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black,
                          blurRadius: 6.0,
                          spreadRadius: 0.5,
                          offset: Offset(
                            0.7,
                            0.7,
                          ),
                        ),
                      ],
                    ),
                    child: CircleAvatar(
                      backgroundColor: Colors.white,
                      child: Icon(Icons.menu, color: Colors.black,),
                      radius: 20.0,
                    ),
                  ),
                ),
              ),

              Positioned(
                left: 0.0,
                right: 0.0,
                bottom: 0.0,
                child: Container(
                  height: 300.0,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(18.0), topRight: Radius.circular(18.0)),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black,
                        blurRadius: 16.0,
                        spreadRadius: 0.5,
                        offset: Offset(0.7, 0.7),
                      )
                    ],
                  ),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 18.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(height: 6.0),
                        Text("Hola,Buen día..", style: TextStyle(fontSize: 12.0),),
                        Text("A dondé quisieras ir?", style: TextStyle(fontSize: 20.0, fontFamily: "Brand-Bold"),),
                        SizedBox(height: 20.0),
                        Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(5.0),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black54,
                                blurRadius: 6.0,
                                spreadRadius: 0.5,
                                offset: Offset(0.7, 0.7),
                              )
                            ],
                          ),
                          child: Padding(
                            padding: const EdgeInsets.all(12.0),
                            child: Row(
                              children: [
                                Icon(Icons.search, color: Colors.blueAccent,),
                                SizedBox(width: 10.0,),
                                Text("Buscar una ubicación de entrega")
                              ],
                            ),
                          ),
                        ),
                        SizedBox(height: 24.0),
                        Row(
                          children: [
                            Icon(Icons.home,color: Colors.grey,),
                            SizedBox(width: 12.0,),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text("Agregar Casa"),
                                SizedBox(height: 4.0,),
                                Text("La dirección de su casa", style: TextStyle(color: Colors.grey[500], fontSize: 12.0),),

                              ],
                            ),
                          ],
                        ),

                        SizedBox(height: 10.0),

                        DividerWidget(),

                        SizedBox(height: 16.0),

                        Row(
                          children: [
                            Icon(Icons.work,color: Colors.grey,),
                            SizedBox(width: 12.0,),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text("Agregar Trabajo"),
                                SizedBox(height: 4.0,),
                                Text("La dirección de su trabajo", style: TextStyle(color: Colors.grey[500], fontSize: 12.0),),

                              ],
                            ),
                          ],
                        ),

                      ],
                    ),
                  ),
                ),
              )
            ],
          ),
        );
      }
    }


<!-- end snippet -->

I hope and you can help giving me a solution or giving me a path so that I can continue because I don't know what to do.

2
  • Do you have this package added to your project? If I remember right, Google Maps on web platform does not work without it. Commented Dec 23, 2021 at 7:30
  • Yes, I have that package, and I continue with my mistake Commented Dec 23, 2021 at 19:15

4 Answers 4

5

The error occurs because you're trying to use google_maps_flutter on web - which only supports Android and iOS. If you're adding Google Maps plugin on Flutter for web, you need to use google_maps_flutter_web

Sign up to request clarification or add additional context in comments.

1 Comment

I believe this answer is no longer relevant. "This package is endorsed, which means you can simply use google_maps_flutter normally. This package will be automatically included in your app when you do, so you do not need to add it to your pubspec.yaml."
4

I also had the same problem. I did the following thing & succeeded.

  1. Enable JavaScript API from Google Cloud Console & install Google maps flutter web.

  2. After installation remember to add the following code snippet on the web/index.html path of flutter project & enter your own API key in the place of "YOUR_API_KEY" text of the below code snippet.

<head>

  <!-- // Other stuff -->

  <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
</head>

2 Comments

This one worked for me! Also don't forget to enable the API Key in the Google Cloud Console under "API Keys".
How does this solve the problem? Will the code have to be re-inserted every time I rebuild the app? What about when I want to build a production build?
1

Make to modify the <head> tag of your web/index.html to load the Google Maps JavaScript API, like so:

<head>

  <!-- // Other stuff -->

  <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
</head>

Comments

0

just add this code before tag in web/index.html in your project

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.