SOUGAT RIYADH
SOUGAT RIYADH

Reputation: 701

Add flutter to existing android app "Cannot resolve symbol Flutter"

I am trying to add flutter module in existing android app from medium articles and some YouTube videos and step by step follows the instructions.But problem is if i try to add this code in MainActivity.java then it shows me an error "Cannot resolve symbol Flutter", means if i type Flutter. then it doesn't show me createView. Is it the code has been changed?

View flutterView=Flutter.createView(MainActivity.this,getLifecycle(),"r1");
FrameLayout.LayoutParams frameLayout=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
addContentView(flutterView,frameLayout);

Upvotes: 1

Views: 1315

Answers (1)

Thaw De Zin
Thaw De Zin

Reputation: 1669

Me too. Flutter.createView is not working. But I found this solution. In your Manifest.xml, add meta-data for Flutter.

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.Fludroid.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="io.flutter.Entrypoint"
            android:value="main"
            />
    </activity>

use FlutterView in your layout file

<io.flutter.embedding.android.FlutterView
    android:id="@+id/flutter_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

attach Flutter in your Activity's onCreate method

if (flutterEngine == null) {
        flutterEngine = FlutterEngine(requireContext())
        flutterEngine!!.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint(FlutterMain.findAppBundlePath(), "main")
        )
    }
    flutterView = view.findViewById(R.id.flutter_view)
    flutterView?.attachToFlutterEngine(flutterEngine!!)

add @pragma('vm:entry-point') in main.dart

@pragma('vm:entry-point')
void main() => runApp(MyApp());

Good Luck.

Upvotes: 2

Related Questions