Flutter中ScreenUtilInit builder参数类型不匹配错误解决
解决ScreenUtilInit builder参数类型不匹配错误
执行pub get后遇到如下错误:The argument type 'MaterialApp Function()' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget?)'
受影响的核心代码:
return ScreenUtilInit( designSize: Size(428.0, 926.0), builder: () => MaterialApp( title: 'MMAS', debugShowCheckedModeBanner: false, theme: ThemeData( textTheme: TextTheme( headline3: TextStyle( fontFamily: 'OpenSans', fontSize: 45.0, color: Colors.deepOrangeAccent, ), button: TextStyle( fontFamily: 'OpenSans', ), subtitle1: TextStyle(fontFamily: 'NotoSans'), bodyText2: TextStyle(fontFamily: 'NotoSans'), ), colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.indigo) .copyWith(secondary: Colors.orange), textSelectionTheme: TextSelectionThemeData(cursorColor: Colors.amberAccent), ), builder: (context, widget) => MediaQuery( data: MediaQuery.of(context).copyWith(textScaleFactor: 1), child: widget!, ), home: SignIn(), // Home(), locale: _locale, localizationsDelegates: [ AppLocalization.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], localeResolutionCallback: (locale, supportedLocales) { for (var supportedLocale in supportedLocales) { if (supportedLocale.languageCode == locale!.languageCode && supportedLocale.countryCode == locale.countryCode) { return supportedLocale; } } return supportedLocales.first; }, supportedLocales: [ Locale("en", "US"), Locale("de", "DE"), Locale("es", "ES"), Locale("fr", "FR"), Locale("hi", "IN"), Locale("ja", "JP"), Locale("ko", "KR"), Locale("pt", "PT"), Locale("ru", "RU"), Locale("tr", "TR"), Locale("vi", "VN"), Locale("zh", "CN"), ], ), );
解决方法
错误原因是新版本flutter_screenutil库中,ScreenUtilInit的builder参数要求函数签名为Widget Function(BuildContext, Widget?),但你的代码里用了无参函数。只需给builder函数补全参数签名即可:
修改后的核心代码部分:
return ScreenUtilInit( designSize: Size(428.0, 926.0), // 补全context和widget参数,即使暂时不用也要保留签名 builder: (context, widget) => MaterialApp( // 原MaterialApp内部代码不变 title: 'MMAS', debugShowCheckedModeBanner: false, // ... 其余代码省略 ), );
新版本的flutter_screenutil调整了builder的参数要求,目的是更好地支持上下文传递和组件嵌套,补全参数后类型匹配错误就会消失。
内容的提问来源于stack exchange,提问作者Buiswer231
相关产品推荐
相关产品推荐

