I have this class
class EntryController {
final TabController? tabController;
final List<DictionaryEntry> history;
EntryController({
required this.history,
this.tabController,
});
EntryController copyWith({
TabController? tabController,
List<DictionaryEntry>? history,
}) {
return EntryController(
history: history ?? this.history,
tabController: tabController ?? this.tabController,
);
}
}
And i have this Riverpod notifier
@riverpod
class ExpandedEntryController extends _$ExpandedEntryController {
@override
EntryController build() {
return EntryController(
history: [],
);
}
void updateState(EntryController controller) => state = controller;
void seeRelatedWord(DictionaryEntry relatedWord) {
state.history.add(relatedWord);
state.tabController!.animateTo(0);
}
void onDispose() {
state.tabController!.dispose();
}
}
I know that by default it has de autoDispose( ) method, but when it disposes i suppose i should dispose the tabController
right? How should i do it?
Thanks
2
Answers
Don’t override
this.onDispose()
!. Addto your
build()
. That should take care of disposing that controller when this controller goes away.Correct your code like this:
You can also use
ref.watch
orref.listen
safely in thebuild
method of your notifier.