Flutter io.flutter.app.FlutterActivity' is already defined in a single-type import
The io.flutter.app.FlutterActivity has been deprecated (in Sep 2020)
Deprecation: FlutterActivity is the new API that now replaces this class. See https://flutter.dev/go/android-project-migration for more migration details.
in order to favor io.flutter.embedding.android.FlutterActivity so to fix the issues:
-
Remove
import io.flutter.app.FlutterActivity;
to useembedding.android.FlutterActivity
-
Use
getFlutterEngine().getDartExecutor().getBinaryMessenger()
instead ofgetFlutterView()
to create and registerMethodChannel
as:new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), "key_01") .setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { if(call.method.equals("startService")){ startService(); } } });
Alternately, you can override configureFlutterEngine
to get the flutterEngine
instance in order to create MethodChannel
.
jdsflk
High school student, interested in Python, C++, Java and Flutter.
Updated on December 26, 2022Comments
-
jdsflk over 1 year
I am developing a Flutter application for Android, which sends daily notifications. To keep things local, I tried to implement this using Android background services. I followed this tutorial: https://www.youtube.com/watch?v=NXuAzXY_KOo
import android.content.Intent; import android.os.Build; import android.os.Bundle; import androidx.annotation.NonNull; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.embedding.android.FlutterActivity; import io.flutter.app.FlutterActivity; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugins.GeneratedPluginRegistrant; public class MainActivity extends FlutterActivity { private Intent foreService; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); FlutterEngineGetter flutterEngineGetter = new FlutterEngineGetter(); GeneratedPluginRegistrant.registerWith(getFlutterEngine()); foreService = new Intent(MainActivity.this, MyService.class); new MethodChannel(getFlutterView(), "key_01") .setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { if(call.method.equals("startService")){ startService(); } } }); } private void startService(){ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ startForegroundService(foreService); } else { startService(foreService); } } }
I get two errors, the first one is io.flutter.embedding.android.FlutterActivity is already defined in a single type of import. As far as I understand the problem, which causes this is that there are two classes called FlutterActivity, but I need both of them, to use getFlutterEngine() and getFlutterView() methods. How could I import only one method from a class?
The second error is Cannot resolve method 'getFlutterView' in 'MainActivity', I think solving the first one would solve this.
Thanks for your time and help in advance!