0

My problem is related to the provider pattern/library.

I have created some state classes as follows (ive replaced content/var names to try and keep it simple)

class State1 with ChangeNotifier{

String _s;

  set s(String newS){
    _s = newS;
    notifyListeners();
  }

}

and then I use a multiprovider to pass it (create the objects in the widget init which acts as parent to everything in the app.)

child: MultiProvider(
        providers: [
          ChangeNotifierProvider(builder: (context) => state1),
        ],
        child: Stack(
        alignment: Alignment.center,
        children: stackChildren,
      ),

Which is then accessed in the children widgets build method using

 state1Var = Provider.of<State1>(context);

And all of this works fine..

My issue is when I get to using a navigation push I can no longer access the state.

onPressed: (() {
          Navigator.push(
            contextField,
            MaterialPageRoute(builder: (context) => NewPage()),
          );
        }),

As i get this error

Could not find the correct Provider<State1> above this NewPage Widget...

I did manage to get it using this

  Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => Consumer(builder: (),child: NewPage(),)),
          );

But when I popped the widget using navigator.pop() the state couldnt be used as it said it had been disposed. Am i doing this incorrectly or is there something i am missng?

Sorry if i've made this complicated. I had to remove a lot of code.

Thanks for your help :)

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
ebg11
  • 906
  • 1
  • 12
  • 33

1 Answers1

3

Probably you have a context hierarchy problem. Wrap your MaterialApp with MultiProvider or your Provider must be on top of the Route you are getting the provider within.

Otherwise, you can wrap your Scaffold with Consumer widget. What Consumer does is, simply it's a builder for establishing a connection between your Provider and Route by creating yet another context and lets you to obtain Provider class via InheritedWidget.

Mehmet Esen
  • 6,156
  • 3
  • 25
  • 44
  • 2
    Ah i hadnt wrapped material app with the multi provider... Thank you so much dude :D – ebg11 Jul 14 '19 at 10:28
  • @ebg11 if your problem is solved, please don't forget to close your topic by accepting the right answer: http://stackoverflow.com/help/someone-answers – Mehmet Esen Jul 14 '19 at 10:32
  • I do have another question if you don't mind answering. I want to set a edit text controllers value in the init() to have content that is in one of the providers. Setting this in the build means they can never change it as it immediately turns back to whats in the state (it does this because i dont want to always update the state when they type something). I have been using this AfterLayoutMixin ( https://pub.dev/packages/after_layout) as a solution to this (provides a method to override that is called once after build). Is there an official way to do this or is this library a good method. – ebg11 Jul 14 '19 at 10:36
  • 1
    @ebg11 I could not understand what exactly you want. Since it's a completely another problem, you can open another question with some codes and better explanation. Tag me in the comment if you want, so I will be notified. – Mehmet Esen Jul 14 '19 at 10:39
  • 1
    Ok I will. i just wrote the question but i cant post for like another 60 min. Also for the notification do i just do this @Esen Mehmet. Thanks for all your help – ebg11 Jul 14 '19 at 11:03
  • Ok ive added the question. – ebg11 Jul 14 '19 at 12:36