Search code examples
androidiosflutterflutter-getx

flutter_screenutil doesn't work with getx


I have been trying to implement flutter_screenutil with getx but it keeps showing an error and won't let the app run. I need getx for routing and screenutil for different screen sizes, if anyone has faced this similar then please help to fix this code.

The error that I am getting is this :

No MediaQuery widget ancestor found.

MyApp widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was: MyApp
  dirty
The ownership chain for the affected widget is: "MyApp ← [root]"

No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.

The relevant error-causing widget was: 
  MyApp MyApp:file:///E:/Projects/winora-service/lib/main.dart:16:31
When the exception was thrown, this was the stack: 
#0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:275:7)

the main.dart file looks like this.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: TabletDetector.isTablet(MediaQuery.of(context))
          ? Size(834, 834)
          : Size(360, 690),
      builder: (_, __) => GetMaterialApp(
        themeMode: ThemeMode.system,
        debugShowCheckedModeBanner: false,
        home: SplashScreen(),
        builder: (context, widget) {
          return MediaQuery(
            //Setting font does not change with system font size
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget!,
          );
        },
      ),
    );
  }
}

Solution

  • You have to remove builder from inside GetMeterialApp and use as widget for this.

    Means, through child go on other widgets not able to call through builder.