Thursday, 24 December 2020

Flutter io.flutter.app.FlutterActivity' is already defined in a single-type import

I am developing a Flutter application for Android, which sends daily notifications. To keep things local, I tried to implement this usind the Android beckground 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 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!



from Flutter io.flutter.app.FlutterActivity' is already defined in a single-type import

No comments:

Post a Comment