Facebook App Events not working in Flutter

I have a flutter application where my task is to log user events using facebook app events. I have added the following package to my pubspec.yaml:

facebook_app_events: ^0.14.7

Added the following in build.gradle:

buildscript {
    ext.kotlin_version = '1.5.10'
    repositories {
        google()
        jcenter()
        mavenCentral() //**Added as per fb docs
    }

Added the following to app/build.gradle:

dependencies {
    implementation 'com.facebook.android:facebook-android-sdk:latest.release'

}

In AndroidManifest, I've added the following meta-data:

<application
    ..//
        <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="app id"/>
     

    </application>

Later, in my flutter views I did the following implementation:

static final facebookSDK = FacebookAppEvents();

TextButton(
                          onPressed: () {
                            facebookSDK.logEvent(
                              name: 'button_clicked',
                              parameters: {
                                'button_id': 'the_clickme_button',
                              },
                            );
                          },
                          child: Text("Trigger Payment Info Click")),

When I click this button, I get the following error instead of sending the logEvent to fb:

E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475): Failed to handle method call
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475): kotlin.UninitializedPropertyAccessException: lateinit property appEventsLogger has not been initialized
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at id.oddbit.flutter.facebook_app_events.FacebookAppEventsPlugin.handleLogEvent(FacebookAppEventsPlugin.kt:99)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at id.oddbit.flutter.facebook_app_events.FacebookAppEventsPlugin.onMethodCall(FacebookAppEventsPlugin.kt:48)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:865)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at android.app.ActivityThread.main(ActivityThread.java:7438)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#flutter.oddbit.id/facebook_app_events(32475):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:989)
E/flutter (32475): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(error, lateinit property appEventsLogger has not been initialized, null, kotlin.UninitializedPropertyAccessException: lateinit property appEventsLogger has not been initialized
E/flutter (32475):  at id.oddbit.flutter.facebook_app_events.FacebookAppEventsPlugin.handleLogEvent(FacebookAppEventsPlugin.kt:99)
E/flutter (32475):  at id.oddbit.flutter.facebook_app_events.FacebookAppEventsPlugin.onMethodCall(FacebookAppEventsPlugin.kt:48)
E/flutter (32475):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (32475):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/flutter (32475):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:865)
E/flutter (32475):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (32475):  at android.os.MessageQueue.next(MessageQueue.java:336)
E/flutter (32475):  at android.os.Looper.loop(Looper.java:174)
E/flutter (32475):  at android.app.ActivityThread.main(ActivityThread.java:7438)
E/flutter (32475):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (32475):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/flutter (32475):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:989)
E/flutter (32475): )
E/flutter (32475): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (32475): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter (32475): <asynchronous suspension>

I deleted .gradle folder, ran flutter clean still no help. I do not know much about native kotlin. Any sort of help will be appreciated.


Solution 1:

If you have followed all the required steps from the package and you get this type of error. Just do the following in your AndroidManifest

Just below the meta data that the package suggests us to add:

<meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>

Add the following code in your manifest just below the above meta data

        <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="appname" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>

Your manifest should look like the following:

<application
    android:name="io.flutter.app.FlutterApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="appname"
    android:usesCleartextTraffic="true">
    
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="appname" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/LaunchTheme"
        android:windowSoftInputMode="adjustResize">
        <!-- Specifies an Android theme to apply to this Activity as soon as
             the Android process has started. This theme is visible to the user
             while the Flutter UI initializes. After that, this theme continues
             to determine the Window background behind the Flutter UI. -->
        <meta-data
            android:name="io.flutter.embedding.android.NormalTheme"
            android:resource="@style/NormalTheme" />
        
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_notification" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>