Flutter/firebase/Analytics
提供: 初心者エンジニアの簡易メモ
firebaseから設定ファイルをDL
- https://console.firebase.google.com
- androidとiosのプロジェクトを作成し、google-services.jsonと、GoogleService-Info.plistをDL
インストール
pubspec.yaml
dependencies: firebase_core: ^2.24.2 firebase_analytics: ^10.7.4
androidの準備
google-services.jsonをandroid/appの下へ
android/build.gradle
buildscript {
repositories {
google() // Google's Maven repository
}
dependencies {
classpath 'com.google.gms:google-services:3.2.1'
}
}
allprojects {
repositories {
google() // Google's Maven repository
}
}
android/app/build.gradle のファイルの末尾へapply~を追加
dependencies {
}
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
iosの準備
xcode でios/Runner.xcworkspaceを開きRunner/RunnerへGoogleService-Info.plistファイルをドラッグする。
サンプル
起動処理にnavigatorObserversを以下を追加する。
app.dart
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
FirebaseAnalytics analytics = FirebaseAnalytics();
return new MaterialApp(
title: 'hogeproject',
home: SplashScreen(),
navigatorObservers: [
FirebaseAnalyticsObserver(analytics: analytics),
],
);
公式サンプル
イベント
static FirebaseAnalytics analytics = FirebaseAnalytics();
Future<void> _sendAnalyticsEvent() async {
await analytics.logEvent(
name: 'test_event',
parameters: <String, dynamic>{
'string': 'string',
'int': 42,
'long': 12345678910,
'double': 42.0,
'bool': true,
},
);
setMessage('logEvent succeeded');
}
iosで以下エラーが出る場合
Terminating app due to uncaught exception 'com.firebase.core', reason: '`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) could not find a valid GoogleService-Info.plist in your project. Please download one from https://console.firebase.google.com/.'
xcodeプロジェクトを開きRunner/Runnerへドラッグする。
