Unact / yandex_mapkit

Flutter implementation of YandexMapkit
MIT License
132 stars 151 forks source link

java.lang.AssertionError: You need to set the API key before using MapKit! #352

Closed AbrorEsonov closed 2 months ago

AbrorEsonov commented 2 months ago

I am using yandex_mapkit with audio_service, I am setting up key like this:

class MainActivity : FlutterFragmentActivity() {
override fun provideFlutterEngine(context: Context): FlutterEngine {
        initMapKit()
        return AudioServicePlugin.getFlutterEngine(this)
    }

    private fun initMapKit() {
        try{
            MapKitFactory.setApiKey("my_api_key")
        } catch (e: Exception){
            Log.d("MapKitFactory", "Error: $e")
            e.printStackTrace()
        }
    }

......

    }

It is working with audio_service but when application is in the terminated state and notification came, when i tap notification white screen appearing. It is working well in background state.

audio_service: ^0.18.12 firebase_messaging: ^14.7.16 yandex_mapkit: ^3.4.0

when notification comes, error in the logs: Received exception while registering java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at lc.a.registerGeneratedPlugins(Unknown Source:21) at io.flutter.embedding.engine.FlutterEngine.(Unknown Source:250) .............................. Caused by: java.lang.AssertionError: You need to set the API key before using MapKit! at com.yandex.mapkit.MapKitFactory.checkApiKey(Unknown Source:15) at com.yandex.mapkit.MapKitFactory.initialize(Unknown Source:6) at la.r.onAttachedToEngine(Unknown Source:4) at ac.b.add(Unknown Source:110) at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(Unknown Source:828)

[✓] Flutter (Channel stable, 3.16.9, on macOS 14.0 23A344 darwin-arm64, locale en-UZ) • Flutter version 3.16.9 on channel stable at /Users/abroresonovmacpro/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 41456452f2 (5 weeks ago), 2024-01-25 10:06:23 -0800 • Engine revision f40e976bed • Dart version 3.2.6 • DevTools version 2.28.5

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/abroresonovmacpro/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15C500b • CocoaPods version 1.14.3

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

DCrow commented 2 months ago

Hello!

You are initializing yandex_mapkit incorrectly. In your example you are initializing in MainActivity.kt Initialization must be made in your applications MainApplication.kt file in onCreate method. A more detailed example is shown here.