federico D'Armini
federico D'Armini

Reputation: 321

Provider in different route Flutter

I wanted to upload a picture inside Firebase Storage when I saw this error :

The provider you are trying to read is in a different route.

So I started study what is this Provider and now I understood that I need to include the page that I'm using to upload the picture inside the same route of the ancestor Provider of type Database.

So I had the issue inside "Registration_form" that is created by "LogInPage" through a Navigator like this :

void navigateToRegistrationForm(context) {
  Navigator.push(context,
      MaterialPageRoute<void>(builder: (BuildContext context) {
    return Reg_Form();
  }));
  return;
}

Now I'm changing the code that is something like :

void navigateToRegistrationForm(context) {
  Navigator.push(
      context,
      MaterialPageRoute<void>(
          builder: (BuildContext context) => Provider<Database>(
                create: (_) => FirestoreDatabase(),
                builder: (context, child) => Reg_Form(),
              )));
  return;
}

But I don't understand what I should pass to the create method...I just want to create Reg_Formin the right route to call Firebase functions to put the file inside firebase storage so I think that FirebaseDatabase isn't what suits my needs.

Upvotes: 1

Views: 3163

Answers (1)

Loren.A
Loren.A

Reputation: 5595

If I understand your issue correctly, you can either move your ChangeNotifierProvider for your Database class higher up in the widget tree by wrapping your MaterialApp like so:

ChangeNotifierProvider<Database> (
  builder: (context) => Database(),
  child: MaterialApp(...

Or just change the create in what you provided to

create: (_) => Database(),

Either way should give you access to the correct Provider/BuildContext in your Registration form with standard context.read<Database>()...

Upvotes: 2

Related Questions