最近我在我的 flutter 项目中添加了 firebase。我使用 dart、flutter fire 和 firebase CLI 按照以下步骤初始化项目。
$ dart pub global activate flutterfire_cli
$ flutterfire configure --project=<my project ID>
然后通过运行添加 firebase 包
$ flutter pub add firebase_core
$ flutter pub add firebase_analytics
在我的 main.dart 中,我已经初始化了 firebase,如下
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
}
为了测试一切是否正常工作,我在 MyApp() 中添加了日志,如下所示
...
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
...
@override
Widget build(BuildContext context) {
analytics.logAppOpen();
当我运行我的应用程序时,我的控制台中出现以下错误
Missing google_app_id. Firebase Analytics disabled. See https:...
我尝试在我的
settings.gradel
中手动添加插件,如下
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
id 'com.google.gms.google-services' version '4.4.1' apply false
}
我的
app/build.gradel
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
id 'com.google.gms.google-services'
}
...
flutter {
source '../..'
}
dependencies {
implementation platform('com.google.firebase:firebase-bom:32.8.0')
implementation 'com.google.firebase:firebase-analytics'
}
我还检查了我的
google-services.json
的应用程序 ID,它是存在的。
"client": [
{
"client_info": {
"mobilesdk_app_id": "X:XXXXXXXXX:android:XXXXXXXXXXXXX",
"android_client_info": {
"package_name": "XXX.XXXXXXXX.XXXXXX"
}
},
我能够通过添加来解决问题
implementation("com.google.firebase:firebase-analytics-ktx")
在我的app/build.gradel
(根据 Flutter Firebase 设置和缺少 google_app_id 添加它。Firebase Analytics 已禁用)
当我重新运行应用程序时,错误消失了,几个小时后我就能够看到分析事件。
这是参考代码
应用程序/build.gradl
flutter {
source '../..'
}
dependencies {
implementation platform('com.google.firebase:firebase-bom:32.8.0')
implementation 'com.google.firebase:firebase-analytics'
implementation("com.google.firebase:firebase-crashlytics") //Firebase Crashlystic
implementation("com.google.firebase:firebase-analytics-ktx")
}
gradel.设置
pluginManagement {
def flutterSdkPath = {
def properties = new Properties()
file("local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
return flutterSdkPath
}
settings.ext.flutterSdkPath = flutterSdkPath()
includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
id 'com.google.gms.google-services' version '4.3.15' apply false
id "com.google.firebase.crashlytics" version "2.8.1" apply false
}
include ":app"