0

I have been trying to make a simple flutter app, have implemented the login logic.

  void _saveForm() async {
    final loginDetails = Provider.of<Login>(context);
    final isValid = _form.currentState.validate();
    if (!isValid) {
      return;
    }
    _form.currentState.save();
    loginDetails.isLoggingIn();
    await loginDetails.logIn(_loginDetails);

    if (loginDetails.getIsLoggedIn()) {
      print("Changing page");
      Navigator.of(context).pushReplacementNamed(ActivityScreen.routeName);
    }
  }

This will simply navigate to different page. Now what i want is, if it is logged in, i could use the loginDetails.getIsLoggedIn() in my another Provider class to start fetching some data.

I tried using Provider.of<Login>(context); but context was not found.

If it's not possible, where should I call for the function in the provider to fetch the data, and since i got a auth token by logging in which i am saving in Login provider, do i have to call the provider of Login to get the auth token and pass everytime with the request.

Is there someting like interceptor in Provider.

Thanks.

Alpit Anand
  • 1,213
  • 3
  • 21
  • 37

1 Answers1

0

If you are using _saveForm() inside a StatelessWidget, just changing it to StatefulWidget and State will solve the problem. But, basically, you can pass the context from the build() function.

/// Inside the StatelessWidget

Widget build(BuildContext context) {
 /// some code
  _saveForm(context);
}

void _saveForm(BuildContext context) async {
    final loginDetails = Provider.of<Login>(context); // now you can use context
    /// Same code you wrote. 
  }
wurikiji
  • 327
  • 3
  • 12