使用 StoreConnector 显示新屏幕时 Flutter 中的 Firebase Analytics

Firebase Analytics in Flutter when using StoreConnector to display new screen

我正在尝试将 Flutter 中的 Firebase Analytics 添加到我的小部件,但我正在使用 StoreConnector 显示 2 个不同的屏幕 Alpha 和 Beta。我如何使用分析来跟踪屏幕变化?

class MyWidgetPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, MyViewModel>(
      converter: (store) => myViewModel.create(store),
      builder: (context, viewModel) {
        return _body(context, viewModel);
      },
    );
  }

  _body(BuildContext context, MyViewModel viewModel) {
    if (viewModel.withLoading) return _loading();
    if (viewModel.withAlphaContent) return _contentAlpha(context, viewModel);
    if (viewModel.withBetaContent) return _contentBeta(context, viewModel);
    if (viewModel.withRetry) return _retry(context);
  }

...

}

一种解决方案是使用 onDidChange 回调,如下所示:

  @override
  Widget build(BuildContext context) {
    FirebaseAnalytics analytics = FirebaseAnalytics();
    return StoreConnector<AppState, MyViewModel>(
      converter: (store) => myViewModel.create(store),
      onDidChange: (viewModel) {
        if (viewModel.withAlphaContent) {
         analytics.setCurrentScreen(screenName: 'alpha content');
        } 
        if (viewModel.withBetaContent) {
         analytics.setCurrentScreen(screenName: 'beta content');
        }
      },
      builder: (context, viewModel) {
        return _body(context, viewModel);
      },
    );
  }