0

I have done everything listed below in the google_maps_flutter package: https://pub.dev/packages/google_maps_flutter. Even added render type in my main file:

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

/// MapsDemo is the Main Application.
class MapsDemo extends StatefulWidget {
  const MapsDemo({super.key});

  @override
  State<MapsDemo> createState() => _MapsDemoState();
}

class _MapsDemoState extends State<MapsDemo> {
  final LatLng sourceLocation = const LatLng(37.422, -122.084);

  late GoogleMapController _mapController;

  @override
  void dispose() {
    _mapController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('GoogleMaps examples')),
      body: SizedBox(
        height: MediaQuery.of(context).size.height,
        child: DefaultTabController(
          length: 2,
          child: Column(
            children: [
              const SizedBox(
                height: 30,
              ),
              const SizedBox(
                height: 40,
                child: TabBar(
                  labelColor: Colors.black,
                  tabs: [
                    Text(
                      "Posts",
                    ),
                    Text(
                      "Maps",
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 20,
              ),
              Expanded(
                child: TabBarView(
                  children: [
                    const Center(
                      child: Text(
                        "Posts Available",
                      ),
                    ),
                    GoogleMap(
                      mapType: MapType.normal,
                      initialCameraPosition: CameraPosition(
                        target: sourceLocation,
                        zoom: 14.5,
                      ),
                      markers: <Marker>{
                        Marker(
                          markerId: const MarkerId('sourceLocation'),
                          position: sourceLocation,
                          infoWindow: const InfoWindow(
                            title: 'Your Location',
                          ),
                          onTap: () {
                            // Handle marker tap event here, if needed
                          },
                        ),
                      },
                      onMapCreated: (GoogleMapController controller) {
                        _mapController = controller;
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  final GoogleMapsFlutterPlatform mapsImplementation =
      GoogleMapsFlutterPlatform.instance;
  if (mapsImplementation is GoogleMapsFlutterAndroid) {
    mapsImplementation.useAndroidViewSurface = true;
    mapsImplementation.initializeWithRenderer(AndroidMapRenderer.legacy);
  }
  runApp(const MaterialApp(home: MapsDemo()));
}

This are the logs that i get on app launch:

D/MapsInitializer( 1186): preferredRenderer: LEGACY
D/zzcb    ( 1186): preferredRenderer: LEGACY
I/zzcb    ( 1186): Making Creator dynamically
I/DynamiteModule( 1186): Considering local module com.google.android.gms.maps_legacy_dynamite:0 and remote module ]
I/DynamiteModule( 1186): Selected remote version of 
V/DynamiteModule( 1186): Dynamite loader version >= 2, using loadModule2NoCrashUtils
W/ProtoDataStoreFlagStore( 1186): Unable to retrieve flag snapshot for com.google.android.gms.maps#com.example.test_project, using defaults.

When i navigate to the maps tab it logs out:

 D/MapsInitializer( 1186): preferredRenderer: null
D/zzcb    ( 1186): preferredRenderer: null

I/TetheringManager( 1186): registerTetheringEventCallback:com.example.test_project
E/le.test_projec( 1186): Invalid ID 0x00000000.
2
E/le.test_projec( 1186): Invalid ID 0x00000000.

When i do so repeatedly it crashes the app (move to another tab and back it crashes the app) and prints the same above logs everytime. Latout of app

  • It looks like your app is still using the `LEGACY` renderer instead of the `LATEST` one. Have you tried reproducing this with the `LATEST` renderer? – Yrll Jul 11 '23 at 05:54
  • Yeah i have tried but still crashes, i think it is because google maps is being rebuilt many times(just speculation). Currently trying to figure out how to render it once. – Kevinhector Jul 12 '23 at 04:31
  • I tried reproducing your code on my end. I don't seem to experience the crash you mentioned. Though I have flutter 3.10 installed. I also made sure that the map is rendered in both LEGACY and LATEST, and there seems to be no problem on my end. – Yrll Jul 12 '23 at 06:03
  • Upgrading to flutter 3.10.5 and using latest renderer did help a bit. But is there a way i can cache the google maps so that it doesn't keep rebuilding when a user moves from one tab to another as per say. Also Thanks in advance @Yrll. – Kevinhector Jul 17 '23 at 09:54
  • 1
    Maybe using IndexedStack should work as mentioned here? https://stackoverflow.com/a/54825616/18422887 – Yrll Jul 17 '23 at 23:53
  • 1
    Surprisingly i had opted to use it and forgot to tell you how it improved the performance. Anyways thanks so much @Yrll. – Kevinhector Jul 19 '23 at 19:24

0 Answers0