首页 > 解决方案 > 当我使用 flutter_screenutil 或 sizer 包时,我无法声明“家”。我收到错误参数“home”未定义

问题描述

这是我的主要飞镖文件的代码:

void main() {
  runApp(new MaterialApp());
}

class MaterialApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints){
        return OrientationBuilder(
          builder: (context, orientation){
            SizerUtil().init(constraints, orientation);
            return new MaterialApp(
              home: OpeningScreen()
            );
          },
        );
      },
    );
  }
}

这是我得到的错误:

The named parameter 'home' isn't defined.  Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'home'.

我有一个呈现应用程序登录页面 UI 的 OpeningScreen() 类。当我也在 flutter_screenutil 中使用“home”时,同样的错误出现了。

标签: flutterflutter-layoutflutter-dependencies

解决方案


ScreenUtil() 没有任何问题。问题就在这里

 class MaterialApp extends StatelessWidget

您不能使用MaterialApp作为类名,因为它是在颤振库中预定义的。尝试使用其他名称,例如 class MyApp extends StatelessWidget

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(360, 690),
      allowFontScaling: false,
      builder: () => MaterialApp(
      
        home: Testing(),
      ),
    );
  }
}

推荐阅读