处理后使用 ChangeNotifier
ChangeNotifier was used after being disposed
我是 Flutter 的新手,一直坚持这个
我有一个页面使用名为 GoogleMapsNotifier 的 class 和 ChangeNotifier,当我弹出页面时,我想在这个 class(最后一个函数)中处理 Stream。
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();
}
}
然后我有一个弹出页面的返回按钮,我之前用 Provider 调用了这个函数。
onTap: () async {
Provider.of<GoogleMapsNotifier>(context, listen: false)
.dispose();
Navigator.pop(context);
},
第一次运行正常,但是当我第二次进入该页面并再次按“返回”按钮时,return 出现错误
Unhandled Exception: A GoogleMapsNotifier was used after being
disposed. E/flutter (13173): Once you have called dispose() on a
GoogleMapsNotifier, it can no longer be used.
我该如何解决这个问题?
Provider
应该在你推送的 Route
里面。如果您使用全局提供程序,GoogleMapsNotifier
的实例将始终相同。因此,您第二次进入该页面时将无法正常工作(因为它是您第一次处理的同一个实例)
这是一个具体的例子
// GOOD
runApp(MaterialApp(...));
...
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ChangeNotifierProvider<GoogleMapsNotifier>(
create: (_) => GoogleMapsNotifier(),
child: ...,
),
),
);
// BAD
runApp(
ChangeNotifierProvider<GoogleMapsNotifier>(
create: (_) => GoogleMapsNotifier(),
child: MaterialApp(
home: ...,
),
)
);
...
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ...,
),
);
我是 Flutter 的新手,一直坚持这个 我有一个页面使用名为 GoogleMapsNotifier 的 class 和 ChangeNotifier,当我弹出页面时,我想在这个 class(最后一个函数)中处理 Stream。
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();
}
}
然后我有一个弹出页面的返回按钮,我之前用 Provider 调用了这个函数。
onTap: () async {
Provider.of<GoogleMapsNotifier>(context, listen: false)
.dispose();
Navigator.pop(context);
},
第一次运行正常,但是当我第二次进入该页面并再次按“返回”按钮时,return 出现错误
Unhandled Exception: A GoogleMapsNotifier was used after being disposed. E/flutter (13173): Once you have called dispose() on a GoogleMapsNotifier, it can no longer be used.
我该如何解决这个问题?
Provider
应该在你推送的 Route
里面。如果您使用全局提供程序,GoogleMapsNotifier
的实例将始终相同。因此,您第二次进入该页面时将无法正常工作(因为它是您第一次处理的同一个实例)
这是一个具体的例子
// GOOD
runApp(MaterialApp(...));
...
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ChangeNotifierProvider<GoogleMapsNotifier>(
create: (_) => GoogleMapsNotifier(),
child: ...,
),
),
);
// BAD
runApp(
ChangeNotifierProvider<GoogleMapsNotifier>(
create: (_) => GoogleMapsNotifier(),
child: MaterialApp(
home: ...,
),
)
);
...
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ...,
),
);