OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Maps cannot be retrieved before calling buildView

  • Thread starter Thread starter Victor Garcia
  • Start date Start date
V

Victor Garcia

Guest
Steps to reproduce Hello,

To reproduce this bug, simply use the package: google_maps_flutter: ^2.5.0, open the library example code and reload the map. When trying to reload the web page, that is, trying to dispose of the controller, the error is reproduced.

Expected results When reloading the page it shouldn't give this error, even in release mode.

I'm encountering an issue with the google_maps_flutter_web plugin: The error happens when I reload the web page. When I dispose of the GoogleMapController in the widget's dispose() method, I subsequently encounter the following error:

Code:
errors.dart:294 Uncaught (in promise) Error: Assertion failed: file:///C:/Users/55169/AppData/Local/Pub/Cache/hosted/pub.dev/google_maps_flutter_web-0.5.4+2/lib/src/google_maps_flutter_web.dart:32:12
controller != null
"Maps cannot be retrieved before calling buildView!"
at Object.throw_ [as throw] (errors.dart:294:3)
at Object.assertFailed (errors.dart:35:3)
at [_map] (google_maps_flutter_web.dart:32:22)
at google_maps_flutter_web.GoogleMapsPlugin.new.updateTileOverlays (google_maps_flutter_web.dart:98:5)
at updateTileOverlays.next ()
at runBody (async_patch.dart:84:54)
at Object._async [as async] (async_patch.dart:127:5)
at google_maps_flutter_web.GoogleMapsPlugin.new.updateTileOverlays (google_maps_flutter_web.dart:94:34)
at [_updateTileOverlays] (controller.dart:147:10)
at google_maps_flutter._GoogleMapState.new._updateTileOverlays$ (google_map.dart:414:25)
at _updateTileOverlays$.next ()
at async_patch.dart:45:50
at _RootZone.runUnary (zone.dart:1661:54)
at _FutureListener.thenAwait.handleValue (future_impl.dart:162:18)
at handleValueCallback (future_impl.dart:846:44)
at _Future._propagateToListeners (future_impl.dart:875:13)
at [_completeWithValue] (future_impl.dart:647:5)
at async._AsyncCallbackEntry.new.callback (future_impl.dart:721:7)
at Object._microtaskLoop (schedule_microtask.dart:40:11)
at _startMicrotaskLoop (schedule_microtask.dart:49:5)
at async_patch.dart:181:7

Actual results Currently this error is continuous, in all possible scenarios, I even loaded the package example code and the error persisted.

Code:

Code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:jm_connect_front/services/machines.dart';
import 'package:provider/provider.dart';
import '../../models/machine_model.dart';
import '../../providers/machine_provider.dart';

class JMMap extends StatefulWidget {
  const JMMap({super.key});

  @override
  State<JMMap> createState() => JMMapState();
}

class JMMapState extends State<JMMap> {
  late final Completer<GoogleMapController> _controller = Completer();
  List<MachineModel>? allMachines;
  Map<MarkerId, Marker> markers = {};
  Timer? timer;
  BitmapDescriptor? machineIcon;
  BitmapDescriptor? machineOnIcon;

  @override
  void initState() {
    super.initState();
    _setCustomMarker();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      timer = Timer.periodic(const Duration(seconds: 1), (timer) async {
        await Machines().getMachines(context);
        if (mounted) {
          allMachines =
              Provider.of<MachineProvider>(context, listen: false).machineItems;
          updateMarkers();
        }
      });
    });
  }

  void _setCustomMarker() async {
    machineIcon = await BitmapDescriptor.fromAssetImage(
        const ImageConfiguration(), 'assets/images/machine_marker.png');
    machineOnIcon = await BitmapDescriptor.fromAssetImage(
        const ImageConfiguration(), 'assets/images/machine_marker_on.png');
  }

  @override
  void dispose() {
    timer!.cancel();
    super.dispose();
  }

  void updateMarkers() {
    final newMarkers = <MarkerId, Marker>{};
    for (var machine in allMachines!) {
      int lastUpdate = DateTime.now()
          .difference(DateTime.parse(machine.updatedAt.toString()))
          .inMinutes;
      // print("Last Update: $lastUpdate");
      final markerId = MarkerId(machine.id.toString());
      final marker = Marker(
        markerId: markerId,
        position: LatLng(machine.latitude, machine.longitude),
        icon: lastUpdate > 2 ? machineIcon! : machineOnIcon!,
      );

      newMarkers[markerId] = marker;
    }

    setState(() {
      markers.clear();
      markers.addAll(newMarkers);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GoogleMap(
        mapType: MapType.hybrid,
        buildingsEnabled: false,
        compassEnabled: false,
        liteModeEnabled: false,
        indoorViewEnabled: false,
        mapToolbarEnabled: false,
        trafficEnabled: false,
        myLocationEnabled: false,
        tiltGesturesEnabled: false,
        zoomControlsEnabled: false,
        myLocationButtonEnabled: false,
        fortyFiveDegreeImageryEnabled: false,
        markers: Set<Marker>.of(markers.values),
        initialCameraPosition: CameraPosition(
          target: LatLng(
            allMachines?.isNotEmpty == true
                ? allMachines![0].latitude
                : -20.90381991398391,
            allMachines?.isNotEmpty == true
                ? allMachines![0].longitude
                : -47.59624569562974,
          ),
          zoom: 18,
        ),
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
    );
  }
}

FLUTTER DOCTOR:

Code:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.4, on Microsoft Windows [versÆo 10.0.22631.2861], locale pt-BR)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.3)
[√] Android Studio (version 2023.1)
[√] VS Code (version 1.85.1)
[√] Connected device (3 available)
[√] Network resources

• No issues found!
<p>Steps to reproduce
Hello,</p>
<p>To reproduce this bug, simply use the package: google_maps_flutter: ^2.5.0, open the library example code and reload the map. When trying to reload the web page, that is, trying to dispose of the controller, the error is reproduced.</p>
<p>Expected results
When reloading the page it shouldn't give this error, even in release mode.</p>
<p>I'm encountering an issue with the google_maps_flutter_web plugin:
The error happens when I reload the web page.
When I dispose of the GoogleMapController in the widget's dispose() method, I subsequently encounter the following error:</p>
<pre><code>errors.dart:294 Uncaught (in promise) Error: Assertion failed: file:///C:/Users/55169/AppData/Local/Pub/Cache/hosted/pub.dev/google_maps_flutter_web-0.5.4+2/lib/src/google_maps_flutter_web.dart:32:12
controller != null
"Maps cannot be retrieved before calling buildView!"
at Object.throw_ [as throw] (errors.dart:294:3)
at Object.assertFailed (errors.dart:35:3)
at [_map] (google_maps_flutter_web.dart:32:22)
at google_maps_flutter_web.GoogleMapsPlugin.new.updateTileOverlays (google_maps_flutter_web.dart:98:5)
at updateTileOverlays.next ()
at runBody (async_patch.dart:84:54)
at Object._async [as async] (async_patch.dart:127:5)
at google_maps_flutter_web.GoogleMapsPlugin.new.updateTileOverlays (google_maps_flutter_web.dart:94:34)
at [_updateTileOverlays] (controller.dart:147:10)
at google_maps_flutter._GoogleMapState.new._updateTileOverlays$ (google_map.dart:414:25)
at _updateTileOverlays$.next ()
at async_patch.dart:45:50
at _RootZone.runUnary (zone.dart:1661:54)
at _FutureListener.thenAwait.handleValue (future_impl.dart:162:18)
at handleValueCallback (future_impl.dart:846:44)
at _Future._propagateToListeners (future_impl.dart:875:13)
at [_completeWithValue] (future_impl.dart:647:5)
at async._AsyncCallbackEntry.new.callback (future_impl.dart:721:7)
at Object._microtaskLoop (schedule_microtask.dart:40:11)
at _startMicrotaskLoop (schedule_microtask.dart:49:5)
at async_patch.dart:181:7
</code></pre>
<p>Actual results
Currently this error is continuous, in all possible scenarios, I even loaded the package example code and the error persisted.</p>
<p>Code:</p>
<pre><code>import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:jm_connect_front/services/machines.dart';
import 'package:provider/provider.dart';
import '../../models/machine_model.dart';
import '../../providers/machine_provider.dart';

class JMMap extends StatefulWidget {
const JMMap({super.key});

@override
State<JMMap> createState() => JMMapState();
}

class JMMapState extends State<JMMap> {
late final Completer<GoogleMapController> _controller = Completer();
List<MachineModel>? allMachines;
Map<MarkerId, Marker> markers = {};
Timer? timer;
BitmapDescriptor? machineIcon;
BitmapDescriptor? machineOnIcon;

@override
void initState() {
super.initState();
_setCustomMarker();
WidgetsBinding.instance.addPostFrameCallback((_) {
timer = Timer.periodic(const Duration(seconds: 1), (timer) async {
await Machines().getMachines(context);
if (mounted) {
allMachines =
Provider.of<MachineProvider>(context, listen: false).machineItems;
updateMarkers();
}
});
});
}

void _setCustomMarker() async {
machineIcon = await BitmapDescriptor.fromAssetImage(
const ImageConfiguration(), 'assets/images/machine_marker.png');
machineOnIcon = await BitmapDescriptor.fromAssetImage(
const ImageConfiguration(), 'assets/images/machine_marker_on.png');
}

@override
void dispose() {
timer!.cancel();
super.dispose();
}

void updateMarkers() {
final newMarkers = <MarkerId, Marker>{};
for (var machine in allMachines!) {
int lastUpdate = DateTime.now()
.difference(DateTime.parse(machine.updatedAt.toString()))
.inMinutes;
// print("Last Update: $lastUpdate");
final markerId = MarkerId(machine.id.toString());
final marker = Marker(
markerId: markerId,
position: LatLng(machine.latitude, machine.longitude),
icon: lastUpdate > 2 ? machineIcon! : machineOnIcon!,
);

newMarkers[markerId] = marker;
}

setState(() {
markers.clear();
markers.addAll(newMarkers);
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: GoogleMap(
mapType: MapType.hybrid,
buildingsEnabled: false,
compassEnabled: false,
liteModeEnabled: false,
indoorViewEnabled: false,
mapToolbarEnabled: false,
trafficEnabled: false,
myLocationEnabled: false,
tiltGesturesEnabled: false,
zoomControlsEnabled: false,
myLocationButtonEnabled: false,
fortyFiveDegreeImageryEnabled: false,
markers: Set<Marker>.of(markers.values),
initialCameraPosition: CameraPosition(
target: LatLng(
allMachines?.isNotEmpty == true
? allMachines![0].latitude
: -20.90381991398391,
allMachines?.isNotEmpty == true
? allMachines![0].longitude
: -47.59624569562974,
),
zoom: 18,
),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
);
}
}
</code></pre>
<p>FLUTTER DOCTOR:</p>
<pre><code>Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.4, on Microsoft Windows [versÆo 10.0.22631.2861], locale pt-BR)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.3)
[√] Android Studio (version 2023.1)
[√] VS Code (version 1.85.1)
[√] Connected device (3 available)
[√] Network resources

• No issues found!
</code></pre>
Continue reading...
 

Latest posts

Top