How to prevent GoogleMap rebuild because of saving state in every user movement - Flutter

Viewed 27

Hi I am using google_maps_flutter: ^2.2.0 and geolocator: ^9.0.1 to show real time location user and I need to get the coordinates of latitude and longitude user in every movement. Because of I need the data of latitude and longitude so I use ValueListenableBuilder as state to save that value and the effect is.. the widget GoogleMap will always rebuild because I will always calling updateMarkerAndCircle function (for saving the states value). So is there a way to prevent GoogleMap to rebuild many times whenever user is moving because I still need the data of coordinates? Here is the code:

void _toggleListening() {
    if (_positionStreamSubscription == null) {
      final positionStream = _geolocatorPlatform.getPositionStream();
      _positionStreamSubscription = positionStream.handleError((error) {
        _positionStreamSubscription?.cancel();
        _positionStreamSubscription = null;
      }).listen((position) => _updatePositionList(
          _PositionItemType.position, position.toString(), position));
      _positionStreamSubscription?.pause();
    }
  }

void _updatePositionList(
      _PositionItemType type, String displayValue, Position posisi) {
    if (posisi != null) {
      if (_controller != null) {
        _controller.animateCamera(CameraUpdate.newCameraPosition(
            new CameraPosition(
                bearing: 192.8334901395799,
                target: LatLng(
                    posisi.latitude.toDouble(), posisi.longitude.toDouble()),
                tilt: 0,
                zoom: 16.00)));
        updateMarkerAndCircle(posisi);
      }
    }
    _positionItems.add(_PositionItem(type, displayValue));
  }

void updateMarkerAndCircle(Position newLocalData) {
    gMaps.value = GMapsModel(
        latUser: newLocalData.latitude,
        longUser: newLocalData.longitude,
        marker: gMaps.value.marker);
  }

and here is how I am showing the maps

ValueListenableBuilder(
                  valueListenable: gMaps,
                  builder: (_, value, __) {
                    print("load mapssss");
                    return GoogleMap(
                        myLocationEnabled: true,
                        zoomGesturesEnabled: true,
                        scrollGesturesEnabled: true,
                        compassEnabled: true,
                        rotateGesturesEnabled: true,
                        mapToolbarEnabled: true,
                        tiltGesturesEnabled: true,
                        gestureRecognizers:
                            <Factory<OneSequenceGestureRecognizer>>[
                          new Factory<OneSequenceGestureRecognizer>(
                            () => new EagerGestureRecognizer(),
                          ),
                        ].toSet(),
                        mapType: MapType.normal,
                        initialCameraPosition: initialLocation,
                        onMapCreated: (GoogleMapController controller) {
                          _controller = controller;
                        });
                  })
0 Answers
Related