Dmitrii Karakhanov
Dmitrii Karakhanov

Reputation: 11

Provider is above widget, but flutter can't find it

Can't figure it out, please help :(

I'm building mobile app on flutter, using multiprovider. Other providers work in the whole app fine, but this specific provider "Order" works only in DeliveryPage widget for some reason. I just add Order order = context.watch<Order>(); in any other widget and get this error:

Error: Could not find the correct Provider above this ProductPage Widget This happens because you used a BuildContext that does not include the provider of your choice. There are a few common scenarios: ...

I'm preeeetty sure my MultiProvider wraps everything just fine (or isn't it?). Here's devtools screenshot: https://prnt.sc/RRwr3wbX49fN

I've checked for any major differences in DeliveryPage and any other pages and found none. I even compared with ProductPage that is very similar, it is also pushed to Navigator pages array the same way: Navigator.of(context, rootNavigator: false).push(MaterialPageRoute(builder: (_) =>ProductPage())); but Order provider works fine on DeliveryPage and throws error on ProductPage.

Thank you.

MAIN.DART:

import '/provider/mainscreen.dart';
import '/provider/skin.dart';
import '/provider/catalog.dart';
import '/provider/lang.dart';
import '/provider/order.dart';
import '/provider/data.dart';



void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await UserSettings.Load();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {

    return
      MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (context) => Data()),
          ChangeNotifierProvider(
            create: (context) => UserCardi()),
          ChangeNotifierProvider(
              create: (context) => Skin()),
          ChangeNotifierProvider(
              create: (context) => Catalog()),
          ChangeNotifierProvider(
              create: (context) => Order()),
          ChangeNotifierProvider(
              create: (context) => Lang()),

          ChangeNotifierProvider(
              create: (context) => MainScreen()),
        ],
        child: MaterialApp(
          title: 'CARDOIT',
          debugShowCheckedModeBanner: false,
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: [
            Locale('en', ''),
            Locale('ru', ''),
            Locale('de', ''),
            Locale('us', ''),
          ],
          localeResolutionCallback: (locale, supportedLocales) {
            if (locale != null && supportedLocales.contains(Locale(locale.languageCode))) {
              return locale;
            } else {
              return const Locale('en', '');
            }
          },
          theme: ThemeData(
            brightness: Brightness.light,
            canvasColor: Colors.transparent,
            primarySwatch: Colors.blue,
            fontFamily: "Montserrat",
          ),

          home: WindowMain(),
          /*
          initialRoute: '/',
          routes: MainScreen.routeBuilders(),
          */
        )
      );

  }
}
class WindowMain extends StatelessWidget{

  @override
  Widget build(BuildContext context) {

    // сразу опередяем язык пользователя
    UserSettings.autoLang(context);

    Skin skin = context.watch<Skin>();
    
    print("main build: " + UserSettings.skinCode);

    return Scaffold(
        backgroundColor: Colors.white,
        appBar: MainAppBar(),
        bottomNavigationBar: Container(
            padding: EdgeInsets.all(0),
            margin: EdgeInsets.all(0),

            decoration: BoxDecoration(
              image: DecorationImage(
                image: skin.bgBar("appbar"),
                fit: BoxFit.cover,
              ),
            ),
            child: SafeArea(
                child: MainBottomNavbar()
            )
        ),
        body:   Body()

    );
  }

}

class Body extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    MainScreen mainScreen = context.watch<MainScreen>();
    return mainScreen.widgetOptions[mainScreen.index];
  }
}

class MainBottomNavbar extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    MainScreen mainScreen = context.watch<MainScreen>();
    Skin skin = context.watch<Skin>();
    return LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {
                double iconWidth = MediaQuery.of(context).size.width / 4;
                double minHeightIcon = iconWidth * 0.67;

                return Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Container(
                      padding: EdgeInsets.all(0),
                      margin: EdgeInsets.all(0),
                      width: iconWidth,
                      child: IconButton(
                          padding: EdgeInsets.all(0),
                          constraints: BoxConstraints(minHeight: minHeightIcon,),
                          icon: Image(
                            image: skin.btnIcon("cart", mainScreen.index == 0),
                            width: iconWidth,
                            fit: BoxFit.fitWidth,
                          ),
                          onPressed: () => mainScreen.setWidget(0)),
                    ), Container(
                      padding: EdgeInsets.all(0),
                      margin: EdgeInsets.all(0),
                      width: iconWidth,
                      child: IconButton(
                          padding: EdgeInsets.all(0),
                          constraints: BoxConstraints(minHeight: minHeightIcon,),
                          icon: Image(
                            image: skin.btnIcon(
                                "favorites", mainScreen.index == 1),
                            width: iconWidth,
                            fit: BoxFit.fitWidth,
                          ),
                          color: Colors.blue,
                          onPressed: () => mainScreen.setWidget(1)),
                    ), Container(
                      padding: EdgeInsets.all(0),
                      margin: EdgeInsets.all(0),
                      width: iconWidth,
                      child: IconButton(
                          padding: EdgeInsets.all(0),
                          constraints: BoxConstraints(minHeight: minHeightIcon,),
                          icon: Image(
                            image: skin.btnIcon("cart", mainScreen.index == 2),
                            width: iconWidth,
                            fit: BoxFit.fitWidth,
                          ),
                          color: Colors.blue,
                          onPressed: () => mainScreen.setWidget(2)
                      ),
                    ),
                      Container(
                        padding: EdgeInsets.all(0),
                        margin: EdgeInsets.all(0),
                        width: iconWidth,
                        child: IconButton(
                            padding: EdgeInsets.all(0),
                            constraints: BoxConstraints(minHeight: minHeightIcon,),
                            icon: Image(
                              image: skin.btnIcon(
                                  "profile", mainScreen.index == 3),
                              width: iconWidth,
                              fit: BoxFit.fitWidth,
                            ),
                            color: Colors.blue,
                            onPressed:
                                () => mainScreen.setWidget(3)
                        ),
                      ),

                    ]);
              }
          );
  }

} ```

Upvotes: 0

Views: 337

Answers (1)

Dmitrii Karakhanov
Dmitrii Karakhanov

Reputation: 11

I found my mistake. I started import filename with Uppercase letter, while it actually starts with lowercase. Turns out you can't do that, but AS IDE won't underline it as an error and flutter debug console won't show anything either.

Upvotes: 1

Related Questions