Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset provider data - Flutter

I'm using multiprovider in my application. Provider works perfectly, it stores the data and it provides perfect events when anything gets changed.

Now, I've this scenario in my app ->

User 1 loggedIn. Now when I'm adding a second account in the same login(Like Gmail - Multi-Account) providers are returning me with older values.

I tried to search for resetting values of providers but couldn't able to find anything related to it.

Tried things but didn't work:

  • Created a new object of providers to reset.
  • Provider.of<LoginProvider>(context).dispose();
like image 810
Harsh Patel Avatar asked Jan 06 '20 06:01

Harsh Patel


1 Answers

To update a Provider value you can call it with .value() constructor and use a state-dependant variable.

class SomeWidgetState extends State<SomeWidget> {
    Logins logins = [];

    void addLogin(Login newLogin) {
        setState((){
            logins = [...logins, newLogin];
        });
    }

    @override
    Widget build(BuildContext context) {
        return Provider.value(
            value: logins,
            child: OtherWidget(),
        );
    }

When calling addLogin the registered value in Provider will be updated.

like image 126
Augustin R Avatar answered Nov 15 '22 04:11

Augustin R