How to Resolve could not find the correct provider above widget In Flutter ?

· 3 min read
How to Resolve could not find the correct provider above widget In Flutter
How to Resolve could not find the correct provider above widget In Flutter

Some time when users logged into the application user id is passed to the widget from there it preforms save to DB and redirects to a new widget. So in this article, we will learn about How to Resolve could not find the correct provider above widget In Flutter?

How to Resolve could not find the correct provider above widget In Flutter?

Try extracting part of your widget tree that lies below Scaffold to a separate widget. The context you are using now is used to build your top-level widget which does not Navigator yet.

The resulting code will look like the below:

return MaterialApp(
      title: title,
      home: Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: LoginWidget()
class LoginWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
            children: <Widget>[
              Container(
                margin: EdgeInsets.all(8.0),
                child: Card(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(8.0))),
                  child: InkWell(
                    onTap: () {
                      var user = Provider.of<UserRepository>(context);
                      user.savePreference(user.user.id, "Something");
                      user.navigateToNewPage(Dashboard(), context);
                      print(user.user.id);
                    },

...

  }
}

Parameter context for Provider.of(context) must be a child of your defined provider.

@override
  Widget build(BuildContext context) {
    // !important here, Scaffold.of(context) returns null
    return Scaffold(
      appBar: AppBar(title: Text('Demo')),
      body: Builder(
        builder: (BuildContext context) {
          return FlatButton(
            child: Text('BUTTON'),
            onPressed: () {
              // here, Scaffold.of(context) returns the locally created Scaffold
              Scaffold.of(context).showSnackBar(SnackBar(
                content: Text('Hello.')
              ));
            }
          );
        }
      )
    );
  }

Users can also take a look at the official document from this link.

Conclusion:

In this article, we have been through How to Resolve could not find the correct provider above widget In Flutter?

Keep Learning !!! Keep Fluttering !!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply