Skip to content

Throw if Provider.of is called with dynamic #261

Description

@reachanil

My main.dart has this -

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return ChangeNotifierProvider(
builder: (_) => Athlete(),
child: MaterialApp(
.......
home: MyHomePage()));

My home page file has -

class MyHomePage extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
body: new Stack(
children: [
GestureDetector(
onTap: () {
bool tmp = Provider.of(context, listen: false).dummy;

When I run this I get -

Error: Could not find the correct Provider above this MyHomePage Widget

To fix, please:

  • Ensure the Provider is an ancestor to this MyHomePage Widget

In my understanding, MaterialApp is the child of ChangeNotifierProvider and MyHomePage is a descendent of MaterialApp, then why is the error showing cannot find Provider above MyHomePage widget? What could be going wrong?

Thanks,
-Anil

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions