【问题标题】:"Null check operator used on a null value" when calling a Provider at dispose()在 dispose() 调用提供程序时“对空值使用空检查运算符”
【发布时间】:2021-11-15 06:59:34
【问题描述】:

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

这里是这个Provider调用的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() 中的这一行,错误就会消失。 我试过了,但没有帮助

打开终端并稳定运行颤振通道。然后运行颤振 升级。运行后,flutter pub cache repair 重新安装 系统缓存中的软件包。然后只需使用清理构建文件夹 干净利落。

Flutter Doctor 没有显示任何问题。 有什么想法吗? 提前致谢。

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

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

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

【问题讨论】:

    标签: flutter flutter-provider google-maps-flutter


    【解决方案1】:

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

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

    现在错误已经消失了。

    【讨论】:

      猜你喜欢
      • 2022-06-26
      • 2021-08-10
      • 2021-08-13
      • 2021-08-04
      • 2021-10-25
      • 2021-08-11
      • 2021-06-15
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多