Usually when I want to use a provider in a view, I call it directly in the view like this :
var _myProvider = Provider.of<MyProvider>(context);
The problem I have is that I want to use a view as a template because my view will be used multiple times in my app, only some content will change.
So the call functions of the provider will be the same but it is the provider that will change. Example in my template view there is a button that will call a function called validation(). This validation function will be present in all the providers but the execution will change.
So I cannot put in my view the call to a particular Provider as shown above because the provider will change
I try in my route to directly call the provider in parameter but it does not work. How to do ?
case RouterName.kTestView: return CupertinoPageRoute( builder: (context) => ChangeNotifierProvider<MyProvider1>( create: (BuildContext context) => MyProvider1(), child: TemplateView( title: "my title", content: ContentView1(), test: Provider.of<MyProvider1>(context).validation(), ), ) );
I’ve this error :
The following ProviderNotFoundException was thrown building Builder(dirty): Error: Could not find the correct Provider<MyProvider1> above this Builder Widget