"Null check operator used on a null value" when calling a Provider at dispose()

Issue

I have a "Null check operator used on a null value" error after leaving the page with Navigator.pop(context).
The stack says the error is here

@override
  void dispose() {
    final googleMapsNotifier =
        Provider.of<GoogleMapsNotifier>(context, listen: false); // !!!THIS LINE CAUSES THE ERROR
     googleMapsNotifier.dispose();
    locationSubscription.cancel();
    boundsSubscription.cancel();
    addressSearchController.dispose();
    super.dispose();
  }

Here is the Class file that this Provider calls

import 'dart:async';

import 'package:dev/model/mapsModels/geometry.dart';
import 'package:dev/model/mapsModels/location.dart';
import 'package:dev/model/mapsModels/place.dart';
import 'package:dev/model/mapsModels/place_search.dart';
import 'package:dev/services/mapsServices/geolocator_service.dart';
import 'package:dev/services/mapsServices/places_service.dart';
import 'package:dev/services/marker_service.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:rxdart/rxdart.dart';

class GoogleMapsNotifier with ChangeNotifier {
  final geolocatorService = GeolocatorService();
  final placesService = PlacesService();
  final markerService = MarkerService();

  Position? currentLocation;
  List<PlaceSearch> searchResults = [];
  StreamController<Place> selectedLocation = BehaviorSubject<Place>();
  StreamController<LatLngBounds> bounds = BehaviorSubject<LatLngBounds>();
  late Place selectedLocationStatic;
  List<Marker> markers = <Marker>[];

  GoogleMapsNotifier() {
    setCurrentLocation();
  }

  setCurrentLocation() async {
    currentLocation = await geolocatorService.determinePosition();
    selectedLocationStatic = Place(
        geometry: Geometry(
          location: Location(
              lat: currentLocation!.latitude, lng: currentLocation!.longitude),
        ),
        name: '',
        vicinity: '');
    notifyListeners();
  }

  searchPlaces(String searchTerm) async {
    searchResults = await placesService.getAutocomplete(searchTerm);
    notifyListeners();
  }

  setSelectedLocation(String placeId) async {
    var sLocation = await placesService.getPlace(placeId);
    selectedLocation.add(sLocation);
    selectedLocationStatic = sLocation;
    searchResults = [];
    markers = [];
    var newMarker = markerService.createMarkerFromPlace(sLocation);
    markers.add(newMarker);
    var _bounds = markerService.bounds(Set<Marker>.of(markers));
    bounds.add(_bounds as LatLngBounds);
    notifyListeners();
  }

  @override
  void dispose() {
 
      selectedLocation.close();
    

    super.dispose();
  }
}

At the same time, the app does NOT crush and it all works fine after this error.
If I remove this line inside dispose(), the error disappears.
I tried this and it did not help

Open your terminal and run flutter channel stable. Then run flutter
upgrade. After that run, flutter pub cache repair to reinstall the
packages in your system cache. Then Just clean the build folder using
flutter clean.

Flutter Doctor shows no issues.
Any ideas what can it be?
Thanks in advance.

UPD:
I found out that even if class GoogleMapsNotifier is absolutely empty, the error remains.
So its the call itself

final googleMapsNotifier = Provider.of<GoogleMapsNotifier>(context, listen: false); 

if its located in dispose() is causing the error.

Solution

I solved it simple.
As the Stream and its dispose() function are located in separate class accessed with Provider, I took a call to it outside the dispose() of the StatefulWidget and placed it to the onTap of a button that pops the context.

onTap: () {
      Provider.of<GoogleMapsNotifier>(context, listen: false).dispose();             
      Navigator.pop(context);
          },

Now the error has gone.

Answered By – Marchuk Anton

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.