Home > Software design >  How can i add 2 functions in void main before run app , in Flutter?
How can i add 2 functions in void main before run app , in Flutter?

Time:07-02

I need to run 2 function inside my main.dart file, before run app. One is the bottom navigation bar, the other one is the easy localization.

My main.dart is:

void main() => runApp(
MyApp()
);

 Future <void> main() async {

 WidgetsFlutterBinding.ensureInitialized();
 await EasyLocalization.ensureInitialized();

runApp(
  EasyLocalization(
    supportedLocales: [Locale('en', 'US'), Locale('it', 'IT'), Locale('fr', 'FR')],
    path: 'assets/translations', // <-- change the path of the translation files
    fallbackLocale: Locale('en', 'US'),
    assetLoader: CodegenLoader(),
    child: MyLangApp(),
   ),
  );
}


 class MyApp extends StatelessWidget {
 @override
  Widget build(BuildContext context){
    return new MaterialApp(
     home: MyBottomNavBar(),
   );
  }
 }

So basically, i cannot have 2 void main inside the main.dart. How can i put the 2 functions together?

CodePudding user response:

There are two separate parts to your question, and the answers are different.

  1. You're making main() async, and that handles your localization. If you want even more control, we had a discussion about using async functions before rendering the first frame on HumpDayQandA this week, and went into how you can extend the native splash screen as long as needed so your functions can finish. It's the episode for 29 June, 2022 on the FlutterCommunity YouTube channel. https://www.youtube.com/watch?v=9KFk4lypFD4

  2. The BottomAppBar issue is probably why you got dinged for a -1 on this question. That's because this isn't how you use BottomAppBar. It goes in a Scaffold. If you want to not have the AppBar on top then just leave it out.

But you don't want to be calling a BottomNavBar as the home parameter of a MaterialApp. Whatever you use for home is going to be the foundational visible widget in your tree, the lowest widget that all others are built on.

Does your whole app fit inside that BottomNavBar? Then you want to put the bar inside of something else that takes up the whole screen and provides a base to build the rest of your app on... Like a Scaffold.

  • Related