riverpod
riverpod copied to clipboard
Cannot use "ref" after the widget was disposed
Describe the bug In our production app from firebase crashlytics, we are getting this exception being logged .i.e Bad state: Cannot use "ref" after the widget was disposed.
Crash Log
Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Bad state: Cannot use "ref" after the widget was disposed.
at ConsumerStatefulElement._assertNotDisposed(consumer.dart:550)
at ConsumerStatefulElement.read(consumer.dart:619)
at StoresMapComponent.build.<fn>(stores_map_component.dart:35)
at _GoogleMapState.onPlatformViewCreated(google_map.dart:441)
To Reproduce We are setting google map controller on onMapCreated callback
GoogleMap(
onMapCreated: (controller) {
ref
.read(
getMapControllerProvider(MapToLoad.stores).notifier,
)
.update(IMapController.googleaMaps(controller));
},
zoomControlsEnabled: false,
myLocationButtonEnabled: false,
buildingsEnabled: false,
myLocationEnabled: true,
markers: markers.values.map((e) => e.toGoogleMapMarker()).toSet(),
initialCameraPosition: cameraPosition.toGoogleMapCameraPosition(),
)
// Map Controller Provider
final getMapControllerProvider = NotifierProvider.autoDispose
.family<GetMapControllerNotifier, IMapController?, MapToLoad>(
() {
return GetMapControllerNotifier();
},
);
class GetMapControllerNotifier
extends AutoDisposeFamilyNotifier<IMapController?, MapToLoad> {
@override
IMapController? build(MapToLoad args) {
return null;
}
void update(IMapController controller) {
state = controller;
}
}
And its being used in another provider
final mapControllerProvider = NotifierProvider.autoDispose
.family<MapControllerNotifier, IMapController?, StoresTypeToFetch>(
() {
return MapControllerNotifier();
},
);
class MapControllerNotifier
extends AutoDisposeFamilyNotifier<IMapController?, StoresTypeToFetch> {
@override
IMapController? build(StoresTypeToFetch args) {
state = ref.watch(getMapControllerProvider(MapToLoad.stores));
ref.listen(
selectedLocationCameraPositionProvider(args),
fireImmediately: true,
(previous, next) {
if (previous != next) {
state?.animateCamera(next);
}
},
);
return state;
}
}
Is there any thing wrong with this approach which is causing this error? Can i get help on this ?
Expected behavior This bug shouldn't appear.