'Error: Could not find the correct Provider<ClientsProvider> above this Clients Widget in flutter?
I'm getting a list of clients from the Api, in a clients' list screen, when I want to Update a client, I navigate through DetailClient Screen than navigating through Edit client screen, the update is working perfect ..but I wanted to navigate back through the first screen "clients list" from the alert dialogue to see the update in the clients' list. the code is shown as bellow:
Future<void> _saveForm() async {
_form.currentState!.save();
if (_client.id!.isNotEmpty) {
await Provider.of<ClientsProvider>(context, listen: false).updateClients(_client.id!, _client);
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Client has been updated!'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: ((context) => Clients())));
},
child: Text('Okay'),
),
],
));
}else{
I had the following error while trying to navigate:
Error: Could not find the correct Provider above this Clients Widget
This happens because you used a BuildContext
that does not include... the provider
thank you in Advance
Solution 1:[1]
You need to wrap your top widget with MultiProvider
to be able to locate your provider across the context.
void main(){
runApp(
MultiProvider(
providers: [
Provider<ClientsProvider>(
create: (_) ClientsProvider(),
)
],
child: YourFirstWidget(),
)
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kerim |