StateNotifierProvider with autodispose run on android, but on flutter web its throw this error :
Can't we call the autodispose func on StateNotifierProvider ?
How to reproduce the error :
create a state notifier provider with autoDispose
:
final homeControllerProvider =
StateNotifierProvider.autoDispose<HomeController, HomeState>(
(ref) => HomeController(ref));
and call it inside ConsumerWidget :
class Home extends ConsumerWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
// this line throw this error
final status = ref.watch(homeControllerProvider);
return Scaffold(
body: Center(
child: Text(status.data.toString()),
),
);
}
}
StateNotifier class :
class HomeController extends StateNotifier<HomeState> {
HomeController(this.ref) : super(const HomeState()) ;
final Ref ref;
void changeData(String data) {
state = state.copyWith(data: data);
}
}
State class :
class HomeState {
const HomeState({this.data = 'hello world'});
final String data;
HomeState copyWith({String? data}) => HomeState(data: data ?? this.data);
}
when i remove the autoDispose
, the error gone.
the doc say autoDispose
destroy the state of a provider when it is no-longer used. lets say i need to destroy this state provider or reset it state later, how to work around this, i am new to riverpod, please help