"Null check operator used on a null value" 在 dispose() 调用 Provider 时

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

我在使用 Navigator.pop(context) 离开页面后出现“空检查运算符用于空值”错误。 堆栈说错误在这里

@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();
  }

这是此提供程序调用的 Class 文件

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();
  }
}

同时,应用程序不会崩溃,并且在出现此错误后一切正常。 如果我删除 dispose() 内的这一行,错误就会消失。 我试过了,但没用

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 没有显示任何问题。 有什么想法吗? 提前致谢。

更新: 我发现即使 class GoogleMapsNotifier 绝对为空,错误仍然存​​在。 所以它的调用本身

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

如果它位于 dispose() 导致错误。

我简单的解决了。 由于 Stream 及其 dispose() 函数位于通过 Provider 访问的单独 class 中,我在 StatefulWidgetdispose() 之外调用了它并将其放置在 onTap 弹出上下文的按钮。

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

现在错误消失了。