Dart使用flutter screenutil时,builder:(context,child)中的常量值无效

icnyk63a  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(127)

我是一个全新的flutter,事实上这是我第一次使用它,我真的不明白为什么dart在使用flutter_screenutil包中的ScreenUtilInit时会对我大喊大叫,invalid constant value错误来自builder: (context, child)。我在网上到处搜索,阅读了pub文档,我仍然找不到tune如何平息这些令人窒息的bug。
下面是代码片段

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,
        );
      },
    );
  }
}

字符串
我真的很感激这里的一些帮助,因为我真的不知道我在这里做错了什么。

guz6ccqo

guz6ccqo1#

GetMaterialApp小部件中删除const关键字。

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,
        );
      },
    );
  }
}

字符串

ru9i0ody

ru9i0ody2#

由于builder函数通常期望基于运行时数据动态地创建新的widgets,因此它不能被视为const,因此在这种情况下ScreenUtilInit不能是const

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

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit( // const removed
      useInheritedMediaQuery: true,
      designSize: Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child){
        return const GetMaterialApp(
          debugShowCheckedModeBanner: false,
          title: "JobMon",
          theme: ThemeData(
            scaffoldBackgroundColor: kLight,
            iconTheme: IconThemeData(color: kDark),
            primaryColor: Colors.grey,
          ),
          home: defaultHome,
        );
      },
    );
  }
}

字符串

相关问题