8

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();
Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
  • Have you tried `return Provider.value(value: , child: ...)`? – Augustin R Jan 06 '20 at 09:29
  • Can you please elaborate? – Harsh Patel Jan 06 '20 at 09:45
  • 1
    I will post an answer for more clarity – Augustin R Jan 06 '20 at 09:51
  • @HarshPatel did you ever solve this issue, as I am sitting with the same issue at the moment? – Elmer May 18 '20 at 12:37
  • 1
    Umm.. So far i couldn't able to resolve the issue but found a temporary solution. Just create method reset() in every provider and try to call it on your LOGOUT or desire function from where you want to clear the provider. Now inside that provider reset method re-initialize all variables. So far I've achieved using this thing. @Elmer – Harsh Patel May 20 '20 at 06:44
  • @HarshPatel, thanks took a similar approach in my post (https://stackoverflow.com/questions/61799239/flutter-provider-reinitialize-model) if anyone needs more clarification. – Elmer May 20 '20 at 07:35

3 Answers3

1

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.

Augustin R
  • 7,089
  • 3
  • 26
  • 54
0

I have found a workaround. To reset the values to default when you initialize the screen, In the init state, assign the provider values to default values.

void initState() {
Future.delayed(Duration.zero, () {
  final musicProvider = Provider.of<MusicProvider>(context, listen: false);
  final artistProvider =
      Provider.of<ArtistProvider>(context, listen: false);
  final localGuestlist =
      Provider.of<LocalGuestlistProvider>(context, listen: false);
  final aboutProvider = Provider.of<AboutProvider>(context, listen: false);

  ///Resttings the value of list with clear method
  musicProvider.clearMusic();
  artistProvider.clearList();

  ///Resetting the to default values
  localGuestlist.updateGuestlistLimit = 140;
  localGuestlist.updateClosingTime = TimeOfDay(hour: 20, minute: 0);
  aboutProvider.updateAbout = '';
});
super.initState();
}
Kaushal Kishore
  • 447
  • 1
  • 5
  • 13
-1

Kaushal's answer was useful, but I did some changes.

My provider:

class BillsProvider with ChangeNotifier {
  double _balance = 0;

  double get balance => _balance;

  void balanceNotifier(List<double> costs) {
    if (costs.isEmpty)
      _balance = 0;
    else {
      _balance = costs.fold<double>(
          0, (previousValue, element) => previousValue + element);
    }

    notifyListeners();
  }
}

In the page:

class _PageState extends State<Page> {
    //Declaration of the variable.
    BillsProvider? billsProvider;

    @override
    void initState() {
        Future.delayed(Duration.zero, () {

            //Initialize the variable.
            billsProvider = Provider.of<BillsProvider>(context, listen: false);

            //Call the setter and send an empty list.
            billsProvider?.balanceNotifier([]);
        });
        
        super.initState();
    }

    ...
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – primo Sep 20 '22 at 08:51