starter_architecture_flutter_firebase icon indicating copy to clipboard operation
starter_architecture_flutter_firebase copied to clipboard

Approach for Firebase Analytic in this setup?

Open FredvanRijswijk opened this issue 5 years ago • 3 comments

What is you advice for implement firebase_analytics in this setup?

FredvanRijswijk avatar Feb 13 '20 14:02 FredvanRijswijk

One possible way of doing this to create a FirbaseAnalyticsService with all your domain-specific analytics APIs. This can be injected inside the MultiProvider at the root of the widget tree.

And it can be passed as a constructor argument to all view models that need it.

View models are a good place to put analytics calls, as they are easy to test.

Makes sense?

bizz84 avatar Feb 13 '20 16:02 bizz84

Yes make sense, I now had it this way, but in problems with the Tabbar items....

      authServiceBuilder: (_) => FirebaseAuthService(),
      databaseBuilder: (_, uid) => FirestoreDatabase(uid: uid),
    ));

class MyApp extends StatelessWidget {
  const MyApp({Key key, this.authServiceBuilder, this.databaseBuilder})
      : super(key: key);
  // Expose builders for 3rd party services at the root of the widget tree
  // This is useful when mocking services while testing
  final FirebaseAuthService Function(BuildContext context) authServiceBuilder;
  final FirestoreDatabase Function(BuildContext context, String uid)
      databaseBuilder;

      static FirebaseAnalytics analytics = FirebaseAnalytics();
  static FirebaseAnalyticsObserver observer =
      FirebaseAnalyticsObserver(analytics: analytics);
      

  @override
  Widget build(BuildContext context) {
    // MultiProvider for top-level services that don't depend on any runtime values (e.g. uid)
    return MultiProvider(
      providers: [
        Provider<FirebaseAnalytics>.value(value: analytics),
        Provider<FirebaseAnalyticsObserver>.value(value: observer),
        Provider<FirebaseAuthService>(
          create: authServiceBuilder,
        ),
      ],
      child: AuthWidgetBuilder(
        databaseBuilder: databaseBuilder,
        builder: (BuildContext context, AsyncSnapshot<User> userSnapshot) {
          return MaterialApp(
            theme: ThemeData(primarySwatch: Colors.green),
            debugShowCheckedModeBanner: false,
            home: AuthWidget(userSnapshot: userSnapshot),

            onGenerateRoute: Router.onGenerateRoute,
          );
        },
      ),
    );
  }
}

this is a null

navigatorObservers: <NavigatorObserver>[observer],

FredvanRijswijk avatar Feb 14 '20 11:02 FredvanRijswijk

You should be able to add your observer:

final observer = Provider.of<FirebaseAnalyticsObserver>(context);

to your BottomNavigationBar's navigationObservers. Not sure what is null in your code.

bizz84 avatar Feb 15 '20 20:02 bizz84