gpt4 book ai didi

flutter 网络 : Is it Firebase Analytics support in Flutter web application?

转载 作者:行者123 更新时间:2023-12-03 14:51:08 29 4
gpt4 key购买 nike

我们可以在 Flutter Web 应用程序中实现 Firebase Analytics 吗?如果没有,还有其他选择吗?

最佳答案

从 firebase 7.0.0 ( https://pub.dev/packages/firebase/versions/7.0.0 ) 开始,您可以在 Flutter Web 应用程序中使用分析。

以下是步骤:

  • 在您的主机页面中初始化 Firebase
  • <body>
    <!-- Initialize Firebase -->
    <script src="/__/firebase/7.6.1/firebase-app.js"></script>
    <script src="/__/firebase/7.6.1/firebase-analytics.js"></script>
    <script src="/__/firebase/init.js"></script>

    <!-- Initialize app -->
    <script src="main.dart.js" type="application/javascript"></script>
    </body>
  • 导入 firebase 包
  • import 'package:firebase/firebase.dart';
  • 此时,您可以通过 analytics() 访问 Analytics 对象。如果你想自动发送页面浏览量,你可以引入一个路由观察者
  • class AnalyticsRouteObserver extends RouteObserver<PageRoute<dynamic>> {

    final Analytics analytics;

    AnalyticsRouteObserver({@required this.analytics});

    void _sendPageView(PageRoute<dynamic> route) {
    var pageName = route.settings.name;
    if (null != analytics) {
    analytics.setCurrentScreen(pageName);
    } else {
    print('pageName: $pageName');
    }
    }

    @override
    void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
    super.didPush(route, previousRoute);
    if (route is PageRoute) {
    _sendPageView(route);
    }
    }

    @override
    void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    if (newRoute is PageRoute) {
    _sendPageView(newRoute);
    }
    }

    @override
    void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
    super.didPop(route, previousRoute);
    if (previousRoute is PageRoute && route is PageRoute) {
    _sendPageView(previousRoute);
    }
    }
    }
  • 最后在您的应用程序中注册路由观察者
  • import 'dart:js' as js;
    ...
    Widget build(BuildContext context) {
    return MaterialApp(
    navigatorObservers: [AnalyticsRouteObserver(analytics: js.context.hasProperty('firebase')?analytics():null)],
    ...
    }

    关于 flutter 网络 : Is it Firebase Analytics support in Flutter web application?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59409441/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com