skip to Main Content

I updated my flutter version to the latest one and since I observed the app can’t get the firebase token and it returns an error [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: The method 'getToken' was called on null.. I am trying to get the token with the below code:

FirebaseMessaging firebaseMessaging ;
String firebaseToken;

  Future<void> firebaseCloudMessaging_Listeners() async {
    
    firebaseMessaging.getToken().then((token){
      firebaseToken = token;
    });
   
  }

I have My pubspec.yaml for firebase_message is firebase_messaging: ^13.0.4 And android/build.gradle dependencies is


buildscript {
    ext.kotlin_version = '1.5.31'
    repositories {
        google()
        jcenter()
        mavenCentral()  // Maven Central repository
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.13'

    }

    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'com.android.support'
                        && !details.requested.name.contains('multidex') ) {
                    details.useVersion "27.1.1"
                }

                if (details.requested.group == 'androidx.core'
                        && !details.requested.name.contains('androidx') ) {
                    //details.useVersion "1.0.1"
               details.useVersion "1.5.0"
                }
            }
        }
    }

}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()  // Maven Central repository
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


And and my app/build.gradle dependencies is


def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "appname"
        minSdkVersion 20
        //noinspection OldTargetApi
        multiDexEnabled true
        targetSdkVersion 33
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}



dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.firebase:firebase-analytics'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:multidex:1.0.3'
    implementation platform('com.google.firebase:firebase-bom:31.0.2')

}


I am not sure where I got it wrong but I will appreciate it if anyone can help in case of any additional info let me know.

2

Answers


  1. Try the following code:

    FirebaseMessaging firebaseMessaging;
    String firebaseToken;
    
    Future<void> firebaseCloudMessaging_Listeners() async {
      final String? fcmToken = await firebaseMessaging.getToken();
    
      if (fcmToken != null) {
        firebaseToken = fcmToken;
      }
    }
    
    Login or Signup to reply.
  2. I implemented this feature as below:

    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
    String firebaseToken;
    
    Future<void> firebaseCloudMessaging_Listeners() async {
       await _firebaseMessaging.getToken().then((value) {
          firebaseToken = token;
       });
    }
    

    Please note You have to add await Firebase.initializeApp(); in main.dart inside mainDelegate() method

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search