skip to Main Content

Am completely new to flutter and as matter of fact this is my first bite in using it and I really can’t figure out why dart is yelling at me when using ScreenUtilInit from flutter_screenutil package, with invalid constant value error coming from builder: (context, child). I have searched everywhere on the web and read the pub documentation and i still couldn’t find tune how to appease these choking bug.

Here is the code snippets

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const ScreenUtilInit(
      useInheritedMediaQuery: true,
      designSize: Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      // The Invalid constant value is coming from the next line
      builder: (context, child){
        return const GetMaterialApp(
          debugShowCheckedModeBanner: false,
          title: "JobMon",
          theme: ThemeData(
            scaffoldBackgroundColor: kLight,
            iconTheme: IconThemeData(color: kDark),
            primaryColor: Colors.grey,
          ),
          home: defaultHome,
        );
      },
    );
  }
}

I will really appreciate some help here cause i don’t really know what am doing wrong here.

2

Answers


  1. Remove the const keyword from GetMaterialApp widget.

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return const ScreenUtilInit(
          useInheritedMediaQuery: true,
          designSize: Size(375, 812),
          minTextAdapt: true,
          splitScreenMode: true,
          //Remove the const keyword
          builder: (context, child){
            return GetMaterialApp(
              debugShowCheckedModeBanner: false,
              title: "JobMon",
              theme: ThemeData(
                scaffoldBackgroundColor: kLight,
                iconTheme: IconThemeData(color: kDark),
                primaryColor: Colors.grey,
              ),
              home: defaultHome,
            );
          },
        );
      }
    }
    
    Login or Signup to reply.
  2. because of defaultHome, kLight and kDark are not const, you can’t define GetMaterialApp as const.

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const ScreenUtilInit(
          useInheritedMediaQuery: true,
          designSize: Size(375, 812),
          minTextAdapt: true,
          splitScreenMode: true,
          builder: (context, child){
            return GetMaterialApp(  // const removed
              debugShowCheckedModeBanner: false,
              title: "JobMon",
              theme: ThemeData(
                scaffoldBackgroundColor: kLight,
                iconTheme: IconThemeData(color: kDark),
                primaryColor: Colors.grey,
              ),
              home: defaultHome,
            );
          },
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search