Facebook 应用程序事件在 Flutter 中不起作用

Facebook App Events not working in Flutter

我有一个 flutter 应用程序,我的任务是使用 facebook 应用程序事件记录用户事件。我已将以下包添加到我的 pubspec.yaml:

facebook_app_events: ^0.14.7

在 build.gradle 中添加了以下内容:

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

将以下内容添加到 app/build。gradle:

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

}

在 AndroidManifest 中,我添加了以下元数据:

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

    </application>

后来在我的flutter views中做了如下实现:

static final facebookSDK = FacebookAppEvents();

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

当我点击这个按钮时,我收到了以下错误,而不是将 logEvent 发送到 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>

我删除了 .gradle 文件夹,运行 flutter clean 仍然没有帮助。我对原生 kotlin 了解不多。任何形式的帮助将不胜感激。

如果您已按照包中的所有必需步骤进行操作,并且收到此类错误。只需在您的 AndroidManifest

中执行以下操作

就在包建议我们添加的元数据下方:

<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>

您的清单应如下所示:

<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>