Open EricMoin opened 3 months ago
I had the same problem when calling snackbar from the main() function, but moving the snackbar call to onInit of a GetXController class that gets created during application startup did the trick and now works perfectly on startup.
Well, let me explain what's going on:
At the time you called the snackbar, the widget tree Overlay was not ready yet. If you use the Snackbar in main(), or onInit(), or initState(), it will trigger an error, perhaps this is not so obvious, but all of this is called before the build process is completed, so how do you Will display a widget if the tree is not ready yet?
But I'm here to give you the solution:
Get.snackbar("dsds", "dsds",instantInit: false);
instantInit: false
will wait for the widget tree to be ready and send the snackbar.
Hope this helps.
Thanks @jonataslaw for this precise feedback. Already implemented this with suggestions 1 & 3. Works perfectly!
Description
class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return GetMaterialApp( debugShowCheckedModeBanner: false, getPages: BYRRouter.routes, initialRoute: BYRRouter.welcome, theme: ThemeData( primaryColor: BYRTheme.greyBackgroundColor, colorScheme: ColorScheme.light( primary: Colors.white ), scaffoldBackgroundColor: BYRTheme.greyBackgroundColor ), home: ScreenUtilInit( minTextAdapt: true, splitScreenMode: true, designSize: const Size(375, 812), child: WelcomeScreen(), ), ); } } /// welcome_screen.dart import 'package:flutter/material.dart'; import 'package:get/get.dart'; class WelcomeScreen extends StatefulWidget { @override State createState() => WelcomeScreenState();
}
class WelcomeScreenState extends State{
@override
void initState() {
super.initState();
Get.snackbar('Title','message');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: (){
} }
Solution(not good)
Well , I search for a long time for the problem.Certainly I know to write in another way is the better solution.So I change my code: