FlutterMain was a class in the Flutter Android embedding library used in earlier versions of Flutter. It played a central role in initializing the Flutter engine and setting up the necessary environment for a Flutter application to run on Android. However, this class has been deprecated and replaced by other classes and mechanisms in the newer versions of Flutter’s Android embedding.

Key Responsibilities of FlutterMain

  1. Engine Initialization: It initialized the Flutter engine.
  2. Resource Loading: It loaded Flutter resources such as assets and the Dart code.
  3. Configuration: It handled the configuration needed for the Flutter engine to run properly.

Why It Was Deprecated

Flutter’s embedding APIs were significantly refactored to improve modularity, flexibility, and to better support new features. As a part of this refactor, FlutterMain was deprecated in favor of more modular classes like FlutterLoader, FlutterEngine, and others that provide a more robust and flexible API.

Replacements for FlutterMain

  1. FlutterLoader: Responsible for loading and initializing the Flutter engine and resources.
  2. FlutterEngine: Represents an instance of the Flutter runtime and provides an API to control the lifecycle of the Flutter engine.
  3. FlutterActivity / FlutterFragment: These are high-level classes that provide out-of-the-box integration of a Flutter UI within an Android activity or fragment.

Example of Modern Flutter Engine Initialization

Instead of using FlutterMain, you would now typically work with FlutterEngine and FlutterLoader.

Before (Using FlutterMain)

import io.flutter.view.FlutterMain;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

// Deprecated way to initialize Flutter
   FlutterMain.startInitialization(this);
   FlutterMain.ensureInitializationComplete(this, null);
 }
}

After (Using FlutterLoader and FlutterEngine)

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.loader.FlutterLoader;
import io.flutter.embedding.android.FlutterActivity;
import android.os.Bundle;

public class MainActivity extends FlutterActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

// Initialize the Flutter environment using FlutterLoader
   FlutterLoader flutterLoader = new FlutterLoader();
   flutterLoader.startInitialization(getApplicationContext());
   flutterLoader.ensureInitializationComplete(getApplicationContext(), null);

// Create and configure a new FlutterEngine
   FlutterEngine flutterEngine = new FlutterEngine(this);
   flutterEngine.getDartExecutor().executeDartEntrypoint(
       DartExecutor.DartEntrypoint.createDefault()
   );

// Attach the Flutter view to the Android activity
   setContentView(flutterEngine.getRenderer().getView());
 }
}

Support On Demand!

Flutter