i wanted to use snackbar without Scaffold . found many similar question doing this same thing but its not working for me for some reason.
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,
),
);
}
}
and also tried this one too but its the same error always
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,
),
);
}
}
Your material app is no longer constant, because it takes a variable as a parameter. a const value must be unchanging and defined at runtime. Your material app can't be constant anymore, you need to remove the const
keyword:
@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,
),
);
}