{"id":10660,"date":"2024-06-19T10:26:56","date_gmt":"2024-06-19T10:26:56","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10660"},"modified":"2024-06-19T10:26:56","modified_gmt":"2024-06-19T10:26:56","slug":"what-is-the-replacement-for-fluttermain","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/flutter\/what-is-the-replacement-for-fluttermain","title":{"rendered":"What&#8217;s the Replacement for FlutterMain"},"content":{"rendered":"<p><strong>FlutterMain<\/strong> 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&#8217;s Android embedding.<\/p>\n<h3>Key Responsibilities of FlutterMain<\/h3>\n<ol>\n<li><strong>Engine Initialization:<\/strong> It initialized the Flutter engine.<\/li>\n<li><strong>Resource Loading:<\/strong> It loaded Flutter resources such as assets and the Dart code.<\/li>\n<li><strong>Configuration:<\/strong> It handled the configuration needed for the Flutter engine to run properly.<\/li>\n<\/ol>\n<h3>Why It Was Deprecated<\/h3>\n<p>Flutter&#8217;s embedding APIs were significantly refactored to improve modularity, flexibility, and to better support new features. As a part of this refactor, <strong>FlutterMain<\/strong> was deprecated in favor of more modular classes like <strong>FlutterLoader<\/strong>, <strong>FlutterEngine<\/strong>, and others that provide a more robust and flexible API.<\/p>\n<h3>Replacements for FlutterMain<\/h3>\n<ol>\n<li><strong>FlutterLoader:<\/strong> Responsible for loading and initializing the Flutter engine and resources.<\/li>\n<li><strong>FlutterEngine:<\/strong> Represents an instance of the Flutter runtime and provides an API to control the lifecycle of the Flutter engine.<\/li>\n<li><strong>FlutterActivity \/ FlutterFragment:<\/strong> These are high-level classes that provide out-of-the-box integration of a Flutter UI within an Android activity or fragment.<\/li>\n<\/ol>\n<h3>Example of Modern Flutter Engine Initialization<\/h3>\n<p>Instead of using <strong>FlutterMain<\/strong>, you would now typically work with <strong>FlutterEngine<\/strong> and <strong>FlutterLoader<\/strong>.<\/p>\n<p><strong>Before (Using FlutterMain)<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nimport io.flutter.view.FlutterMain;\r\nimport io.flutter.embedding.android.FlutterActivity;\r\n\r\npublic class MainActivity extends FlutterActivity {\r\n @Override\r\n protected void onCreate(Bundle savedInstanceState) {\r\n   super.onCreate(savedInstanceState);\r\n\r\n\/\/ Deprecated way to initialize Flutter\r\n   FlutterMain.startInitialization(this);\r\n   FlutterMain.ensureInitializationComplete(this, null);\r\n }\r\n}<\/pre>\n<p><strong>After (Using FlutterLoader and FlutterEngine)<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nimport io.flutter.embedding.engine.FlutterEngine;\r\nimport io.flutter.embedding.engine.loader.FlutterLoader;\r\nimport io.flutter.embedding.android.FlutterActivity;\r\nimport android.os.Bundle;\r\n\r\npublic class MainActivity extends FlutterActivity {\r\n @Override\r\n protected void onCreate(Bundle savedInstanceState) {\r\n   super.onCreate(savedInstanceState);\r\n\r\n\/\/ Initialize the Flutter environment using FlutterLoader\r\n   FlutterLoader flutterLoader = new FlutterLoader();\r\n   flutterLoader.startInitialization(getApplicationContext());\r\n   flutterLoader.ensureInitializationComplete(getApplicationContext(), null);\r\n\r\n\/\/ Create and configure a new FlutterEngine\r\n   FlutterEngine flutterEngine = new FlutterEngine(this);\r\n   flutterEngine.getDartExecutor().executeDartEntrypoint(\r\n       DartExecutor.DartEntrypoint.createDefault()\r\n   );\r\n\r\n\/\/ Attach the Flutter view to the Android activity\r\n   setContentView(flutterEngine.getRenderer().getView());\r\n }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10662,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-10660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flutter"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10660"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10660"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10660\/revisions"}],"predecessor-version":[{"id":10664,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10660\/revisions\/10664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10662"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}