I’m trying to log user events using firebase_analytics I followed the documentation
this my main() method
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
name: "test_app",
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
This is MyApp class
class MyApp extends StatelessWidget {
const MyApp({super.key});
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
static FirebaseAnalyticsObserver observer =
FirebaseAnalyticsObserver(analytics: analytics);
@override
Widget build(BuildContext context) {
return
...
navigatorObservers: <NavigatorObserver>[observer],
routes: {
'/': (context) => const MainScreen(),
'product': (context) => ProductScreen(
analytics: analytics,
observer: observer,
),
},
),
);
}
}
And this is how I log event
body: TextButton.icon(
onPressed: () async {
await analytics.logEvent(
name: 'test_event',
parameters: <String, dynamic>{
'string': 'string',
},
);
},
icon: const Icon(Icons.analytics),
label: const Text("Test Analytics")),
why do I end up with this error
W/FA ( 7136): '' is an invalid event name.
2
Answers
My problem was in Firebase initialization
The documentation said to initialize Firebase like this but then I did I run into error that I needed to add a name
so I tried to add a name as above
this let the app start but I got the error when I tried to log any event
so I removed options and name from Firebase initialization line
and it worked fine.
if you haven’t imported
Firebase Analytics
, importfirst in your main file.
If the issue still persists, run the following commands: