ProviderNotFoundException (Error: Could not find the correct Provider<LayoutData> above this SchedulingPage Widget using layoutBuilder

Issue

I’m getting a providerNotFoundException, and I suspect that there is a context mismatch in the below code, but I’m having a hard time seeing it. As I understand, problems with BuilderContext arise when an .of method is performed within the same build method, but I don’t see this happening in this case. Some of the Provider.of methods work fine as annotated in the below code, but as soon as SchedulingPage is called, the Provider.of methods no longer work.

What is the problem here?

Edit: I updated to use my full code below:
Here’s the full error: ProviderNotFoundException (Error: Could not find the correct Provider above this LoginForm Widget

void main() {
  runApp(
    Home(),
  );
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        // ChangeNotifierProvider(create: (context) => CalendarData()),
        ChangeNotifierProvider(create: (context) => LayoutData()),
      ],
      child: MyApp(),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
          print("constraints: $constraints");
          Size mediaSize = MediaQuery.of(context).size;
          double safeAreaSize = mediaSize.height - constraints.maxHeight;
          Provider.of<LayoutData>(context).safeAreaDiff = safeAreaSize;
          Provider.of<LayoutData>(context).safeArea = constraints;
          Provider.of<LayoutData>(context).mediaArea = mediaSize;
          var test = Provider.of<LayoutData>(context).mediaArea.width;
          print(test);  // this works

          return Scaffold(body: LoginScreen());
        }),
      ),
    );
  }
}

    class LoginScreen extends StatelessWidget {
      const LoginScreen({Key key}) : super(key: key);

      @override
      Widget build(BuildContext context) {
        double test = Provider.of<LayoutData>(context).mediaArea.width;
        print("test: $test");  // this works
        return LoginForm();
      }
    }

    class LoginForm extends StatefulWidget {
      LoginForm({Key key}) : super(key: key);

      @override
      _LoginFormState createState() => _LoginFormState();
    }

    class _LoginFormState extends State<LoginForm> {
      @override
      Widget build(BuildContext context) {
        double width = Provider.of<LayoutData>(context).mediaArea.width; // The code fails here
        print('width: $width');
        return Text("this is where it fails ^^^^^^^^");
      }
    }

    class LayoutData with ChangeNotifier {
      double safeAreaDiff = 0.0;
      BoxConstraints safeArea;
      Size mediaArea;

      LayoutData() {
        initializeApp();
      }

      void initializeApp() {
        print("layout initialized");
      }
    }

Solution

You can not access provider in same class in which you create. That must be parent widget.

void main() {
  runApp(Home());
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => CalendarData()),
        ChangeNotifierProvider(create: (context) => LayoutData()),
      ],
      child: MyApp(),
    );
  }
}


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
          print("constraints: $constraints");
          Size mediaSize = MediaQuery.of(context).size;
          double safeAreaSize =
              mediaSize.height - constraints.maxHeight; // works
          Provider.of<LayoutData>(context).safeAreaDiff =
              safeAreaSize; // works
          Provider.of<LayoutData>(context).safeArea = constraints; // works
          Provider.of<LayoutData>(context).mediaArea = mediaSize; // works
          Provider.of<CalendarData>(context).working = "beer"; // works
          print(Provider.of<CalendarData>(context).working); // works

          return Scaffold(body: SchedulingPage());
        }),
      ),
    );
  }
}

Output:

Performing hot restart...                                               
Restarted application in 1,181ms.
I/flutter (25187): constraints: BoxConstraints(w=411.4, h=659.4)
I/flutter (25187): layout initialized
I/flutter (25187): 411.42857142857144
I/flutter (25187): test: 411.42857142857144
I/flutter (25187): width: 411.42857142857144

Answered By – Viren V Varasadiya

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.