0

I want to use firebase_messaging for push notifications in flutter. In docs there is Application.java file but my flutter project uses kotlin file format.How to change this java code to kotlin type:


import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
  @Override
  public void onCreate() {
    super.onCreate();
    FlutterFirebaseMessagingService.setPluginRegistrant(this);
  }

  @Override
  public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
  }
}


Error is this: Expecting a top level declaration

  • Couldn't you write it yourself or use the java to kotlin intellij converter? And looks like there is a hidden character in there probably it is a bug in VSCode if you are using, see https://stackoverflow.com/questions/55017476/android-kotlin-error-expecting-a-top-level-declaration-task-appbuildinfog – Animesh Sahu May 18 '20 at 06:17

1 Answers1

5

I beleve that documentation it's not upgraded, they not provide the Kotlin way, but I convert java to Kotlin using the converter on this page https://try.kotlinlang.org

package io.flutter.plugins.firebasemessagingexample
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application:FlutterApplication(), PluginRegistrantCallback {
  fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }
  fun registerWith(registry:PluginRegistry) {
    GeneratedPluginRegistrant.registerWith(registry)
  }
}
Ronaldo Matos
  • 331
  • 1
  • 11