error: Invalid constant value. while setting global key for scaffoldMessengerKey
error: Invalid constant value. while setting global key for scaffoldMessengerKey
我想在没有 Scaffold 的情况下使用 snackbar 。发现很多类似的问题都在做同样的事情,但由于某种原因它对我不起作用。
Future<void> main() async {
await dotenv.load(fileName: '.env');
runApp(const HomeScreens());
}
class HomeScreens extends StatefulWidget {
const HomeScreens({Key? key}) : super(key: key);
@override
_HomeScreensState createState() => _HomeScreensState();
}
class _HomeScreensState extends State<HomeScreens> {
late final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create:(BuildContext context) => MyCurrentTrack(),
child: const MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey, // <= this
home: LoginScreen(),
debugShowCheckedModeBanner: false,
),
);
}
}
也试过这个,但总是出现同样的错误
class _HomeScreensState extends State<HomeScreens> {
final GlobalKey<ScaffoldMessengerState> rootScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create:(BuildContext context) => MyCurrentTrack(),
child: const MaterialApp(
scaffoldMessengerKey: rootScaffoldMessengerKey, // <= this
home: LoginScreen(),
debugShowCheckedModeBanner: false,
),
);
}
}
您的 material 应用不再是常量,因为它采用变量作为参数。 const 值必须是不变的并且在运行时定义。您的 material 应用程序不能再保持不变,您需要删除 const
关键字:
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create:(BuildContext context) => MyCurrentTrack(),
// child: const MaterialApp(
child: MaterialApp( // no const keyword
scaffoldMessengerKey: rootScaffoldMessengerKey,
home: LoginScreen(),
debugShowCheckedModeBanner: false,
),
);
}
我想在没有 Scaffold 的情况下使用 snackbar 。发现很多类似的问题都在做同样的事情,但由于某种原因它对我不起作用。
Future<void> main() async {
await dotenv.load(fileName: '.env');
runApp(const HomeScreens());
}
class HomeScreens extends StatefulWidget {
const HomeScreens({Key? key}) : super(key: key);
@override
_HomeScreensState createState() => _HomeScreensState();
}
class _HomeScreensState extends State<HomeScreens> {
late final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create:(BuildContext context) => MyCurrentTrack(),
child: const MaterialApp(
scaffoldMessengerKey: scaffoldMessengerKey, // <= this
home: LoginScreen(),
debugShowCheckedModeBanner: false,
),
);
}
}
也试过这个,但总是出现同样的错误
class _HomeScreensState extends State<HomeScreens> {
final GlobalKey<ScaffoldMessengerState> rootScaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create:(BuildContext context) => MyCurrentTrack(),
child: const MaterialApp(
scaffoldMessengerKey: rootScaffoldMessengerKey, // <= this
home: LoginScreen(),
debugShowCheckedModeBanner: false,
),
);
}
}
您的 material 应用不再是常量,因为它采用变量作为参数。 const 值必须是不变的并且在运行时定义。您的 material 应用程序不能再保持不变,您需要删除 const
关键字:
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create:(BuildContext context) => MyCurrentTrack(),
// child: const MaterialApp(
child: MaterialApp( // no const keyword
scaffoldMessengerKey: rootScaffoldMessengerKey,
home: LoginScreen(),
debugShowCheckedModeBanner: false,
),
);
}