Menu

App Initialization

Relevant source files

This document describes the startup and bootstrapping process of the Flutter Mobile Project Template application. It covers how the application initializes its core dependencies, sets up state management, and configures essential services before rendering the UI.

For information about routing configuration after initialization, see Routing and Navigation.

Overview

The application initialization process is responsible for setting up the core dependencies and configurations needed for the app to run properly. This includes initializing Flutter bindings, setting up provider overrides, configuring build information, and preparing the application for rendering.

Sources: apps/app/lib/main.dart19-37 apps/app/lib/app_initializer.dart15-53

Initialization Sequence

The application initialization follows a specific sequence to ensure all dependencies are properly set up before the UI is rendered.

1. Flutter Binding Initialization

The first step is to initialize Flutter bindings, which connects the Flutter framework to the device's native platform.

WidgetsFlutterBinding.ensureInitialized();

Sources: apps/app/lib/main.dart20

2. License Registration

The application registers its license information:

LicenseRegistry.addLicense(() async* {
  yield _yumemiMobileProjectTemplateLicense();
});

Sources: apps/app/lib/main.dart22-24

3. Provider Initialization

The core of the initialization process happens in the AppInitializer class, which prepares the necessary provider overrides.

Sources: apps/app/lib/app_initializer.dart18-53

The AppInitializer.initialize() method performs the following steps:

  1. Calls _initializeProviders() to prepare the list of provider overrides
  2. Gets package information via PackageInfo.fromPlatform()
  3. Initializes shared preferences with SharedPreferences.getInstance()
  4. Creates an AppBuildConfig instance with app information
  5. Creates provider overrides for sharedPreferencesProvider and buildConfigProvider
  6. Returns these overrides as part of InitializedValues

Sources: apps/app/lib/app_initializer.dart18-22 apps/app/lib/app_initializer.dart25-52

4. App Build Configuration

The AppBuildConfig class implements the BuildConfig interface and stores essential information about the application:

PropertyDescription
appNameThe name of the application
packageNameThe package/bundle identifier
versionThe app version string
buildNumberThe build number
buildSignatureThe build signature
flavorThe app flavor (development, staging, production)
installerStoreThe app store that installed the app (if available)

Sources: apps/app/lib/app_build_config.dart3-44

The flavor is determined from environment variables:

flavor = Flavor.values.byName(appFlavor);

This uses the enum extension from the core package to convert a string to the corresponding enum value.

Sources: apps/app/lib/app_build_config.dart12 packages/cores/core/lib/src/extension/enum.dart1-12

5. Application Launch

After initialization, the app is launched using runApp() with a ProviderScope that includes the initialized provider overrides:

runApp(
  ProviderScope(
    overrides: [
      ...overrideProviders,
    ],
    child: const MainApp(),
  ),
);

Sources: apps/app/lib/main.dart29-36

MainApp Configuration

The MainApp widget is a ConsumerWidget that serves as the root of the application UI. It configures:

  1. Router: Uses routerProvider to configure navigation
  2. Theme Mode: Uses themeModeNotifierProvider to determine light/dark theme
  3. Exception Handling: Listens to appExceptionNotifierProvider to show error messages
  4. Force Update Checking: Listens to forceUpdateProvider to handle required app updates

Sources: apps/app/lib/main.dart39-105

The MaterialApp.router is configured with:

  1. Localization Delegates: From multiple feature packages
  2. Supported Locales: From multiple feature packages
  3. Scaffold Messenger Key: For global snackbar management
  4. Router Config: From the router provider
  5. Themes: Light and dark themes
  6. Debug Shortcuts: Keyboard shortcuts for debugging (only in debug mode)

Sources: apps/app/lib/main.dart69-103 packages/features/setting/lib/l10n.dart1-3

Summary

The app initialization process follows a clean, modular approach:

  1. Initialize Flutter bindings
  2. Register licenses
  3. Initialize core providers via AppInitializer
    • Package information
    • Shared preferences
    • Build configuration
  4. Set up the application with provider overrides
  5. Configure the MainApp with routing, theming, localization, and exception handling

This structure ensures that all dependencies are properly initialized before the app UI is rendered, providing a solid foundation for the application's functionality.

Sources: apps/app/lib/main.dart19-37 apps/app/lib/app_initializer.dart15-53 apps/app/lib/app_build_config.dart3-44