The main function in Flutter

Gonçalo Palma
Flutter Community
Published in
10 min readJan 16, 2020

--

Photo by Bill Oxford on Unsplash

Every time we start a new Flutter project, there’s a single line of code that most of the times we don’t need to change. This line ensures that our Flutter app is started and shown to the user.

We can read this line of code as a simple sentence: our main function for this project will run an app called MyApp. As with many other programming languages, the main function is the entry point of our application, telling Dart where it should start running our code.

And though this works perfectly for some apps, we may encounter some issues as our projects grows in scale and complexity, namely:

  • This command will only run the MyApp widget with no given arguments, but what if we want a different backend environment to be called or what if we have different versions of the app that we want to test and deploy?
  • What if we need to check the user preferences before the app runs? For example, how can we verify what color scheme our user picked before showing our first screen? Or how can we check if the user is logged in in order to choose what screen we should show first?

--

--

Gonçalo Palma
Flutter Community

Author, Flutter Lead Developer @Pvotal Tech. Organizer, Google Developer Expert for Flutter and Dart, Lead Flutter Developer. 🌍 https://gpalma.pt/