-2

Error: Could not find the correct Provider«FavoriteModel above this Consumer Widget This happens because you used a "BuildContext, that does not include the provider Eror: Could not find the correct Provider«FavoriteModels above this Consumer«FavoriteModels Widget This happens because you used a 'BuildContext that does not include enter image description here

void main() {
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    ),
  );
  APICallService.getProduct();
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => FavoriteModel(),
        ),
        ChangeNotifierProvider(
          create: (context) => CartProvider(),
        ),
      ],
      child: const App(),
    ),
  );
}

I dont know about this error

Mearaj
  • 1,828
  • 11
  • 14
  • Hi @AnasMughal, please provide App widget code as the error appears to be occuring from inside it and also in your code it's FavoriteModel and in your error it's FavoriteModels. Make sure this typo is not there in your code otherwise it appears to be the cause of your. If it's fixed, then kindly close the question else please provide [minimal reproducilble example App's code](https://stackoverflow.com/help/minimal-reproducible-example). Thanks :) – Mearaj Jul 28 '23 at 00:18
  • check: https://stackoverflow.com/questions/57124258/could-not-find-the-correct-provider-above-this-widget – Benyamin Jul 28 '23 at 00:53
  • 1
    This the code of app widget ' import 'package:flutter/material.dart'; class App extends StatelessWidget { const App({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return const MaterialApp( debugShowCheckedModeBanner: false, home: Home(), ); } }@Mearaj – Anas Mughal Jul 28 '23 at 14:45
  • You should use consumer. Please refer to the [official docs](https://pub.dev/packages/provider). Here is the solution. class App extends StatelessWidget { const App({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Consumer2( builder: (BuildContext context, FavoriteModel value, CartProvider value2, Widget? child) { return const MaterialApp( debugShowCheckedModeBanner: false, home: Home(), ); }, ); } } – Mearaj Jul 28 '23 at 16:27
  • If it's solved then please close this issue as it's only implementation issue from your side. Thanks :) – Mearaj Jul 28 '23 at 16:27

1 Answers1

0

Whenever you use Provider use in MaterialApp not in runApp.

  • Try to wrap Material App with Provider because Material App is never disposable throughout the app and you can access all the Provider easily. Just read the provider where you need to use them.
void main() {
  // TODO: implement main
  runApp(MyApp());
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => FavoriteModel(),
      builder: (context, child) {
        return MaterialApp(
          theme: ThemeData(
            useMaterial3: true,
          ),
          home: const App(),
        );
      },
    );
   }
}


AJJ
  • 46
  • 3