I have added Firebase Analytics to a game I have developed as a Flutter web project and can see real-time data bars coming through for users in the last 30 minutes, but cannot see any historic user or event data. I added Firebase to my project yesterday and had a number of users access it, but cannot see any record of those events today.
I have gone through the set-up process again to check that everything is set up correctly and I believe that it is.
Accessing the web site this morning, I saw a real-time record of me accessing the app as below:
However, the historic user activity over time graph shows nothing:
I have created a Singleton to be able to access the analytics instances as follows:
// Singleton class to hold the Analytics for for Firebase
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:septuple/firebase_options.dart';
class Analytics {
Analytics._();
// Generate an instance of the firebase analytics class
final FirebaseAnalytics analytics = FirebaseAnalytics.instance;
// Create an instance of this class as a singleton
static final Analytics _instance = Analytics._();
// Initialise firebase for the app
static Future<void> init() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
getData().setAnalyticsCollectionEnabled(true);
}
//Get the FirebaseAnalystics object to do event logging
static FirebaseAnalytics getData() {
return _instance.analytics;
}
}
I have initialised this in my main() method as follows:
void main() async {
//
// Initialise the Hive database
await Hive.initFlutter();
await Hive.openBox('septupleBox');
//
// Initialise firebase in the Analytics singleton to enable event logging
//
await Analytics.init();
//
// Run the app
runApp(Game());
}
I am logging events in my state manager class, for example when the user guesses the word, as follows:
void setWin(int attempts) {
// Log end of game and that the user guessed correctly
Analytics.getData().logLevelEnd(
levelName: 'septuple',
success: attempts,
);
gameState = GameState.win;
_box.put(_gameStateName, gameState.toString());
_game.gameTimer.startCountdown();
notifyListeners();
}
"logLevelEnd is a default event type so I am assuming that this should just appear in Firebase Analytics. Do I need to configure anything so that it actively captures and retains this?
I am using the following dependencies for the project:
dependencies:
flutter:
sdk: flutter
hive: ^2.2.3
hive_flutter: ^1.1.0
responsive_sizer: ^3.1.1
#flutter_launcher_icons: ^0.10.0
toggle_switch: ^2.0.1
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
firebase_core: ^2.3.0
firebase_analytics: ^10.0.6
And finally, I have a firebase_options.dart file that is configured for web.
For completeness, here is the output from flutter doctor -v:
flutter doctor -v
[√] Flutter (Channel stable, 3.3.9, on Microsoft Windows [Version 10.0.22621.819], locale en-GB)
• Flutter version 3.3.9 on channel stable at D:\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b8f7f1f986 (10 days ago), 2022-11-23 06:43:51 +0900
• Engine revision 8f2221fbef
• Dart version 2.18.5
• DevTools version 2.15.0
[√] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1)
• Android SDK at C:\Users\LaptopBob\AppData\Local\Android\Sdk
• Platform android-33, build-tools 32.0.0-rc1
• ANDROID_HOME = C:\Users\LaptopBob\AppData\Local\Android\Sdk
• ANDROID_SDK_ROOT = D:\Users\LaptopBob\Android_SDK\avd
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.20)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.11.32929.386
• Windows 10 SDK version 10.0.19041.0
[√] Android Studio (version 2021.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[√] VS Code (version 1.73.1)
• VS Code at C:\Users\LaptopBob\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.54.0
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.819]
• Chrome (web) • chrome • web-javascript • Google Chrome 108.0.5359.71
• Edge (web) • edge • web-javascript • Microsoft Edge 107.0.1418.56
[√] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
As per this post, I have waited 24 hours for the data to come through (it is now 26 hours since I started getting bars on the "Users in last 30 minutes" panel).
Does anyone have any ideas why I am not seeing historic user or event data in Firebase Analytics?
The solution to this problem is "wait". The data turned up some time between 30 and 42 hours after the first event was posted to Firebase. Events from the following day also showed up (only 18 hours delay), so maybe there is an initial, longer delay when you first use the service. The only reference to updates I could find in the Firebase Analytics documentation says that the data is updated 'periodically' so I'm not sure how often the data will be updated.
Related
I use get_it for simple dependency injection. I have a function that I made sure is called when the app initializes as below:
void initDI() {
// ... the other providers and repositories ...
//--------- //
// Firebase //
//--------- //
// i reference these instances everywhere when i wanna access them
// such as providers, repositories, etc.
GetIt.I.registerLazySingleton<FirebaseAuth>(() => FirebaseAuth.instance);
GetIt.I.registerLazySingleton<FirebaseFirestore>(
() => FirebaseFirestore.instance,
);
GetIt.I.registerLazySingleton<FirebaseFunctions>(
() => FirebaseFunctions.instance,
);
}
In my test directory, I have a file named initializers.dart, which has setup functions for tests for convenience. One that concerns us is as follows:
// test/initializers.dart
Future<void> initFirebaseEmulatorUse() async {
// ensure widget bindings get initialized because firebase requires this for some reason
WidgetsFlutterBinding.ensureInitialized();
// initialize firebase app
await Firebase.initializeApp();
// use functions emulator because there is no mock or fake package for functions, which is reasonable
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);
// unregister default references
GetIt.I.unregister<FirebaseAuth>();
GetIt.I.unregister<FirebaseFirestore>();
// register with mocks and fakes
GetIt.I.registerLazySingleton<FirebaseAuth>(() => MockFirebaseAuth());
GetIt.I.registerLazySingleton<FirebaseFirestore>(
() => FakeFirebaseFirestore(),
);
}
And, an example test is as follows:
// import initializers, we'll need them
import '../initializers.dart';
void main() {
// use in setUpAll, once and for all
late FirebaseFirestore firebaseFirestore;
setUpAll(() {
// returning future here because i've seen setUpAll must return
// an instance of Future when it has to do something in the future
// so, marking it as async is not enough it seems
return Future(() async {
WidgetsFlutterBinding.ensureInitialized(); // just to be double sure
initDI();
await initFirebaseEmulatorUse();
firebaseFirestore = GetIt.I.get(); // get an instance of firestore
});
});
tearDownAll(() {
return Future(() async {
// ... do something to clear all documents in firestore? ...
// ... so it won't affect other tests? ...
// i need to access `firebaseFirestore` instance here to do it
});
});
test('do tests', () async {
// ... test stuff ...
});
}
However, this fails saying:
PlatformException(channel-error, Unable to establish connection on channel., null, null)
FirebaseCoreHostApi.initializeCore
✖ (setUpAll)
LateInitializationError: Local 'firebaseFirestore' has not been initialized.
dart:_internal LateError._throwLocalNotInitialized
main.<fn>.<fn>
===== asynchronous gap ===========================
dart:async new Future
main.<fn>
✖ (tearDownAll)
Exited (1)
When I go to the line where it throws this error, it actually throws in the tearDownAll.
Troubleshooting for a while, I think we've got two problems:
firebaseFirestore is not initialized while in tearDownAll. While I have it initialized in setUpAll, that probably means the Future I return from setUpAll does not finish fast enough until I reach tearDownAll. That's my guess.
That "Unable to establish connection on channel" message is, I believe, because somehow the Firebase cannot connect to my local emulator.
I've been trying to solve these with different methods for hours but couldn't find a way to solve it. Any blog post about setting up Firebase Emulator + Unit Testing is also appreciated. The posts that I've found so far couldn't get me figure out how I can connect to my local Firebase emulator. Thanks in advance.
Environment
Firebase Tools 11.19.0
get_it: ^7.2.0
firebase_core: ^2.3.0
firebase_auth: ^4.1.3
cloud_firestore: ^4.1.0
firebase_crashlytics: ^3.0.6
cloud_functions: ^4.0.5
firebase_analytics: ^10.0.6
fake_cloud_firestore: ^2.1.0 as dev
firebase_auth_mocks: ^0.9.2 as dev
Flutter Doctor
[✓] Flutter (Channel stable, 3.3.9, on Fedora Linux 36 (KDE Plasma) 6.0.14-200.fc36.x86_64, locale en_US.UTF-8)
• Flutter version 3.3.9 on channel stable at /home/erayerdin/.local/lib/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b8f7f1f986 (4 weeks ago), 2022-11-23 06:43:51 +0900
• Engine revision 8f2221fbef
• Dart version 2.18.5
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /home/erayerdin/.sdks/android/
• Platform android-33, build-tools 33.0.0
• ANDROID_SDK_ROOT = /home/erayerdin/.sdks/android
• Java binary at: /var/lib/snapd/snap/android-studio/125/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Chrome - develop for the web
• Chrome at google-chrome
[✓] Linux toolchain - develop for Linux desktop
• clang version 14.0.5 (Fedora 14.0.5-2.fc36)
• cmake version 3.25.1
• ninja version 1.10.2
• pkg-config version 1.8.0
[✓] Android Studio (version 2021.3)
• Android Studio at /var/lib/snapd/snap/android-studio/125/android-studio
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] VS Code
• VS Code at /snap/code/current
• Flutter extension version 3.56.0
[✓] Connected device (2 available)
• Linux (desktop) • linux • linux-x64 • Fedora Linux 36 (KDE Plasma) 6.0.14-200.fc36.x86_64
• Chrome (web) • chrome • web-javascript • Google Chrome 108.0.5359.124
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
I am building a flutter app for Android and iOS. The challenge I am facing is that whenever I make any changes to the code and save, the app falls back to the initial route. It doesn't matter whether I use stateless or stateful widgets.
My flutter version:
Flutter 2.9.0-1.0.pre.465 • channel master • https://github.com/flutter/flutter.git
Framework • revision 2104a6d97f (3 hours ago) • 2022-01-24 05:05:21 -0500
Engine • revision 8204850dbe
Tools • Dart 2.17.0 (build 2.17.0-49.0.dev) • DevTools 2.9.2
My Doctor:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel master, 2.9.0-1.0.pre.465, on Microsoft Windows [Version 10.0.22000.466], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1)
[√] IntelliJ IDEA Community Edition (version 2020.3)
[√] VS Code (version 1.63.2)
[√] Connected device (3 available)
[√] HTTP Host Availability
• No issues found!
My Main:
void main() async {
if (Platform.isAndroid) {
runApp(const AndroidApp());
} else if (Platform.isIOS) {
runApp(const IosApp());
}
}
Everything else works well but I have several pages in my application and I have to navigate to each page I am working on each time I make a change. This also affects my input fields.
Please help.
convert your widget to a stateful widget and put your variable that contains your navigator key out of the build method of your widget, this will work
I got this error while dealing with Flutter web and Firebase Storage:
Error: TimeoutException after 0:00:05.000000: Future not completed
at Object.createErrorWithStack (http://localhost:65534/dart_sdk.js:4478:12)
at Object._rethrow (http://localhost:65534/dart_sdk.js:37394:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:65534/dart_sdk.js:37388:13)
Then followed by this error (always in pairs)
TypeError: T.as is not a function
at _Future.new.[_setValue] (http://localhost:60098/dart_sdk.js:32317:11)
at Function._propagateToListeners (http://localhost:60098/dart_sdk.js:32660:30)
at async._AsyncCallbackEntry.new.callback (http://localhost:60098/dart_sdk.js:32357:27)
at Object._microtaskLoop (http://localhost:60098/dart_sdk.js:37220:13)
at _startMicrotaskLoop (http://localhost:60098/dart_sdk.js:37226:13)
at http://localhost:60098/dart_sdk.js:32848:9
I have spent the whole 2 days to figure out where this comes from.
So far with the best of my knowledge it seems to come from when I upload an image to Firebase Storage:
Here is the code:
final uploadTask = _storageRef.child(_path).put(_file,
fb.UploadMetadata(
contentType: _file.type, customMetadata: customMetaData));
// Listening to the progress.
final StreamSubscription<fb.UploadTaskSnapshot> stream =
uploadTask.onStateChanged.listen((event) => event);
stream.onDone(() {
uploadTask.cancel();
stream.cancel();
});
And just in case someone asks, here's my flutter doctor -v
[✓] Flutter (Channel dev, 1.20.0-0.0.pre, on Mac OS X 10.15.5 01, locale
en-US)
• Flutter version 1.20.0-0.0.pre at /Applications/flutter
• Framework revision d9653445f4 (7 days ago), 2020-06-09 18:43:03 -0400
• Engine revision e8c13aa012
• Dart version 2.9.0 (build 2.9.0-14.0.dev 5c1376615e)
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Applications/Android/sdk
• Platform android-29, build-tools 29.0.3
• ANDROID_HOME = /Applications/Android/sdk
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 11.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.4.1, Build version 11E503a
• CocoaPods version 1.9.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 47.0.2-dev.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.46.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.11.0
[✓] Connected device (2 available)
• Web Server • web-server • web-javascript • Flutter Tools
• Chrome • chrome • web-javascript • Google Chrome 83.0.4103.106
• No issues found!
Any help is appreciated. Thank you
I just encountered this issue too; it seems to be caused by some quirk in the JS-interop feature of dart (I created an issue in dart-lang/sdk attempting to get some clarification!)
The root cause of this particular problem was an issue with the internal implementation of the onStateChanged Stream in the UploadTask class, here.
The original code was defining the onCompletion callback of the UploadTask as:
var onCompletion = allowInterop(() => _changeController.close());
But whatever .close() was returning was unexpected in the js-interop layer and causing this crash, so I replaced it by:
var onCompletion = allowInterop(() {
_changeController.close();
});
Which did the trick, at least on the firebase_storage_web plugin I'm working on!
The fix has been published as firebase:^7.3.1
PS: Looking at the package:firebase code, I bet the OP wouldn't have any issues if they used uploadTask.future, instead of uploadTask.onStateChanged, but of course you can't have upload progress then. Docs.
I would be sounding silly but it is a real problem with me. I started flutter about a month ago and whenever I upload my app on my android device , I have never run it on an emulator ,it seems to remember its state not that state management one. I will explain what I mean.
Suppose I have my flutter with a text and appbar. So I run my app on my device It runs perfectly. Then I added a floating action button and I hot reloaded my app all is fine the floating action button is in my app. So I closed my pc and then I wanted to look at my app again So I opened it from my phone and the floating action button is gone only text and appbar.
I even uninstalled my app and reinstalled it using flutter run but there is still that problem.
Here is the output of flutter doctor
[√] Flutter (Channel beta, 1.19.0-4.1.pre, on Microsoft Windows [Version 6.3.9600], locale en-IN)
• Flutter version 1.19.0-4.1.pre at C:\flutt\flutter
• Framework revision f994b76974 (6 days ago), 2020-06-09 15:53:13 -0700
• Engine revision 9a28c3bcf4
• Dart version 2.9.0 (build 2.9.0-14.1.beta)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Users\Anupam Karn\AppData\Local\Android\sdk
• Platform android-29, build-tools 29.0.2
• Java binary at: C:\Program Files\Android\Android Studio2\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio2
• Flutter plugin version 45.1.1
• Dart plugin version 192.8052
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[√] VS Code
• VS Code at C:\Users\Anupam Karn\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.11.0
[√] Connected device (3 available)
• SM G600FY • cc07c483 • android-arm • Android 6.0.1 (API 23)
• Web Server • web-server • web-javascript • Flutter Tools
• Chrome • chrome • web-javascript • Google Chrome 83.0.4103.97
• No issues found!
Please help me sort out this issue it happens with every app I make
When you hot reload, no new apk is generated. The source code from the changed files is compiled into kernel files and sent to the mobile device’s Dart VM. The installed apk is untouched.
Read more here:
https://flutter.dev/docs/development/tools/hot-reload
So if you change something, hot reload, close the app and reopen the app it will be the old app again. If you only do flutter run (without build), you install the same old apk again. You need to run flutter build first to have an updated apk.
When I hot reload the web app, I get this error.
This is the flutter doctor -v output:
[√] Flutter (Channel dev, v1.10.14, on Microsoft Windows [Version 6.1.7601], locale en-US)
• Flutter version 1.10.14 at F:\flutter
• Framework revision 1946fc4 (2 weeks ago), 2019-10-07 15:23:31 -0700
• Engine revision 1d62160fdb
• Dart version 2.6.0 (build 2.6.0-dev.5.0 d6c6d12ebf)
[X] Android toolchain - develop for Android devices
X Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/setup/#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.
You may also want to add it to your PATH environment variable.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Android Studio (version 3.5)
• Android Studio at F:\AndroidStudio
• Flutter plugin version 40.2.2
• Dart plugin version 191.8423
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[√] Connected device (2 available)
• Chrome • chrome • web-javascript • Google Chrome 77.0.3865.120
• Headless Server • headless-server • web-javascript • Flutter Tools
! Doctor found issues in 1 category.
It seems that this is a known issue for Flutter web. As mentioned in this thread, the bug have been fixed.
All the listed issues have been fixed. Closing.
Another exception was thrown: NoSuchMethodError: invalid member on null: 'findRenderObject'
#41284 -> DDC issue
Text editing broken in firefox in non-release builds #43450 -> DDC issue
[[web]Chrome][mobile] text editing state is transfered between fields #42769 ->
Chrome
bug