Reputation: 2791
How can I clear the current state of my providers manually in my Flutter app? The use case I have is when a user signs out of my app then signs up as a new/different user the previous users state is still stored in the providers, it is cleared automatically when the app is restarted however.
Upvotes: 6
Views: 9733
Reputation: 165
The solution for me was to reset the values in the init state. Code: https://stackoverflow.com/a/73732182/16684431.
Upvotes: 0
Reputation: 276977
You can use keys to hard-reset the state of a subtree.
And if you want to reset the subtree only partially, you can add a GlobalKey on the top of the other key.
In the end you'll have:
Widget build(BuildContext context) {
return Provider(
key: ObjectKey(someIdentifier),
builder: (_) => Foo(),
child: SomeSubtree(
key: GlobalObjectKey(context),
),
);
}
In such case, if someIdentifier
changes, the state of Provider
will reset but Subtree
will be preserved.
Upvotes: 5