0

StateNotifierProvider with autodispose run on android, but on flutter web its throw this error :

enter image description here

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

My Car
  • 4,198
  • 5
  • 17
  • 50
Sayyid J
  • 1,215
  • 1
  • 4
  • 18
  • 2
    It's a known Dart bug. A workaround will be released in 2.1.0. Either wait for that new version or use a lower Riverpod version – Rémi Rousselet Oct 31 '22 at 11:11
  • @RémiRousselet thanks for response, i am your fan <3, riverpod is beautiful just like flutter. is there a way to dispose other than autodispose, "manually"? – Sayyid J Oct 31 '22 at 11:26

0 Answers0