Skip to content

No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). #333

Closed

Description

@kishangohel

This is breaking whole application.
This kind of updates tested first and release.

please add solution if any one find.

@override
  Widget build(BuildContext context) {
    return DevicePreview(
      enabled: kDebugMode,
      builder: (context) => ScreenUtilInit(
        designSize: const Size(411, 683), // with reference of pixel 2 5.0"
        minTextAdapt: true,
        splitScreenMode: true,
        builder: () => GetMaterialApp(
          useInheritedMediaQuery: true,
          locale: DevicePreview.locale(context),
          builder: DevicePreview.appBuilder,
          debugShowCheckedModeBanner: false,
          initialRoute: AppRoutes.home,
          getPages: AppRoutes.routes,
          theme: lightTheme,
          darkTheme: darkTheme,
          themeMode: ThemeMode.light,
        ),
      ),
    );
  }

Activity

  1. fly-qp commented on Jan 31, 2022

    @fly-qp

    Experiencing the same problem on version 5.1.0. Downgrading to version 5.0.4 fixed this issue for me.

  2. kishangohel commented on Feb 1, 2022

    @kishangohel
    Author

    @lizhuoyuan please suggest or add working solution as most of developers getting same error after upgrading to 5.1.1
    I have tried both solutions mentioned in document but not working.
    I have static file which has all Dimensions with static key

  3. techouse commented on Feb 1, 2022

    @techouse
    Contributor

    Duplicate of #329

    @kishangohel check my comment here how I dealt with the issue #329 (comment)

  4. error404sushant commented on Feb 3, 2022

    @error404sushant

    Remove device preview and wrap Screen Util with MaterialApp. That will fix the crash.

  5. BahelanBaishan commented on Feb 3, 2022

    @BahelanBaishan
    // main.dart
    void main() {
      runApp(const APP());
    }
    
    class APP extends StatelessWidget {
      const APP({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return GetMaterialApp(
          builder: (context, widget) {
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: widget!,
            );
          },
          home: ScreenUtilInit(
            designSize: const Size(360, 690),
            minTextAdapt: true,
            splitScreenMode: true,
            builder: () {
              // ScreenUtil.setContext(context);
              return Theme(
                data: ThemeData(
                  textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)),
                ),
                child: SignPage(),
              );
            },
          ),
        );
      }
    }
    
    //sign_page.dart
    class SignPage extends StatelessWidget {
      SignPage({Key? key}) : super(key: key);
    
      final ctrl = Get.put(SignCtrl());
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              "汉字 ABC abc !@#%^&",
              style: TextStyle(fontSize: 10.sp),
            ),
          ),
        );
      }
    }
    

    这样可以解决问题,我想可能是(flutter2.8)的问题,更改了某些渲染问题?
    This can solve the problem. I think it may be the problem of (fluent 2.8), which has changed some rendering problems?

  6. W2NJL commented on Feb 4, 2022

    @W2NJL

    This was absolutely destroying me tonight and I thought it was something I did. So glad to find this thread.

  7. kishangohel commented on Feb 4, 2022

    @kishangohel
    Author

    Remove device preview and wrap Screen Util with MaterialApp. That will fix the crash.

    I want device preview to test multiple device UI behaviour. So I can't remove it.

  8. sohrabonline commented on Feb 6, 2022

    @sohrabonline
    Contributor

    add ScreenUtil.setContext(context); in builder

      @override
      Widget build(BuildContext context) {
        return ScreenUtilInit(
          designSize: const Size(360, 690),
          minTextAdapt: true,
          builder: () {
            return GetMaterialApp(
              builder: (context, widget) {
                **ScreenUtil.setContext(context);**
                return MediaQuery(
                    data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                    child: widget!);
              },
              debugShowCheckedModeBanner: false,
              theme: settingService.getThemeData(context),
              home: SplashPage(),
            );
          },
        );
      }
    
  9. error404sushant commented on Feb 7, 2022

    @error404sushant

    Found the fix
    Main.dart

    void main() {
    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
    .then((_) {
    runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: App()));
    });
    }

    App.dart

    return ScreenUtilInit(
    builder: (){
    return MaterialApp(
    theme: ThemeData(
    fontFamily: "LatoRegular"
    ),
    debugShowCheckedModeBanner: false,
    routes: {
    '/splash':(context)=>SplashScreen()
    },initialRoute: "/splash",
    );
    },
    //designSize: Size(screenWidth,screenHeight),
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions