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.
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.
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()); } }