Type mismatch: inferred type is MainActivity but FlutterEngine was expected
Solution 1
Please remove GeneratedPluginRegistrant.registerWith(flutterEngine);
from MainActivity.kt
and update as follows.
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
Solution 2
To pass flutter Engine value we can use provideFlutterEngine method. And for flutterView we can use flutterEngine.dartExecutor. Below is the code snippet.
class MainActivity: FlutterActivity() {
private val CHANNEL = "com.startActivity/testChannel"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
provideFlutterEngine(this)?.let { GeneratedPluginRegistrant.registerWith(it) }
MethodChannel(flutterEngine?.dartExecutor,CHANNEL).setMethodCallHandler{ call, result ->
if(call.method.equals("StartSecondActivity")){
val intent= Intent(this,KotlinActivity::class.java)
startActivity(intent)
result.success("ActivityStarted")
}
else{
result.notImplemented()
}
}
}}

Ihsan ALGUL
Works with Java - C# - Dart - Python - JS HTML - CSS - Bootstrap Flutter Uses Android Studio - Visual Studio - InteliJ - Eclipse Loves IntelliJ, SPRING-BOOT
Updated on December 23, 2022Comments
-
Ihsan ALGUL 5 months
I am new on flutter and I want change wallpaper of the device but that needs Method calls from platform channel https://developer.android.com/reference/android/Manifest.permission?hl=en#SET_WALLPAPER and native android wallpaperManager
under the android folder /MainActivity.kt Kotlin file throws an error.
I tried these below:
- A new fresh project with MainActivity.java file
- flutter clean
- flutter pub cache repair
I read about other type mismatch error questions from here but unfortunately not found anything.
Any help appriciated. My code is as below.
MainActivity.kt
package com.combasis.wallpaper_son_app import android.os.Bundle import io.flutter.app.FlutterActivity import io.flutter.plugin.common.MethodChannel import io.flutter.plugins.GeneratedPluginRegistrant import java.io.IOException import android.app.WallpaperManager import android.graphics.BitmapFactory import java.io.File import android.os.Build import android.annotation.TargetApi import android.content.Context import io.flutter.Log private const val CHANNEL = "com.combasis.wallpaper_son_app" class MainActivity: FlutterActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) GeneratedPluginRegistrant.registerWith(this) MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result -> if (call.method == "setWallpaper") { val arguments = call.arguments as ArrayList<*> val setWallpaper = setWallpaper(arguments[0] as String, applicationContext, arguments[1] as Int) if (setWallpaper == 0) { result.success(setWallpaper) } else { result.error("UNAVAILABLE", "", null) } } else { result.notImplemented() } } } @TargetApi(Build.VERSION_CODES.ECLAIR) private fun setWallpaper(path: String, applicationContext: Context, wallpaperType: Int): Int { var setWallpaper = 1 val bitmap = BitmapFactory.decodeFile(path) val wm: WallpaperManager? = WallpaperManager.getInstance(applicationContext) setWallpaper = try { wm?.setBitmap(bitmap, null, true, wallpaperType) 0 } catch (e: IOException) { 1 } return setWallpaper } }
main.dart:...
static const platform = const MethodChannel('com.combasis.wallpaper_son_app'); Future<void> _setWallpaper(int wallpaperType, String url) async { var file = await DefaultCacheManager().getSingleFile(url); try { final int result = await platform .invokeMethod('setWallpaper', [file.path, wallpaperType]); print('Wallpaper Updated.... $result'); } on PlatformException catch (e) { print("Failed to Set Wallpaper: '${e.message}'."); } }
Run:
Launching lib/main.dart on AOSP on IA Emulator in debug mode... Running Gradle task 'assembleDebug'... e: /Users/username/AndroidStudioProjects/walpaperz_app/wallpaper_son_app/android/app/src/main/kotlin/com/combasis/wallpaper_son_app/MainActivity.kt: (21, 48): Type mismatch: inferred type is MainActivity but FlutterEngine was expected FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 13s Exception: Gradle task assembleDebug failed with exit code 1
-Android Studio version: 4.0.1
-Kotlin version: 1.3.72-release-Studio4.0-5 stable
-Flutter version: 1.17.2 stable
-
Ihsan ALGUL almost 3 yearsThanks for your reply, I tried but result failed same as the mention above.