skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    

    so I tried to add a name as above

      await Firebase.initializeApp(
        name: "test_app",
        options: DefaultFirebaseOptions.currentPlatform,
      );
    

    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

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
    
      await Firebase.initializeApp();
    
      runApp(const MyApp());
    }
    

    and it worked fine.


  2. if you haven’t imported Firebase Analytics, import

    import 'package:firebase_analytics/firebase_analytics.dart';
    

    first in your main file.

    If the issue still persists, run the following commands:

    cd android
    ./gradlew clean
    ./gradlew build
    flutter clean
    flutter pub get
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search