Flutter数据报告插件easy_report的使用

Flutter数据报告插件easy_report的使用

easy_report 插件提供了一种简单的方式来报告和管理用户生成的内容,如用户、帖子、评论、照片、聊天等。它适用于任何包含用户生成内容的应用。

TODO

  • 让管理员列出已报告的用户及其内容,并决定是否禁用他们的账户,从而阻止他们继续使用应用。

概念

  • 它不要求用户提供输入来避免给用户带来不便。相反,它会提示用户按一个按钮来选择报告的原因。它提供了三个选项:“垃圾信息”、“滥用”、“其他”,这些选项已经足够了。

  • 它保存以下信息:

    • 负责数据的用户的 UID。这可以是创建数据的用户的 UID 或数据管理员的 UID。
    • 数据的路径。数据可以是实时数据库的路径或 Firestore 文档的路径。
    • 数据的类型。它可以是 userpostcommentphotochat 等。
    • 报告的原因。它可以是 spam(垃圾信息)、abusive(滥用)、other(其他)。
    • 报告的摘要。它可以是一段描述报告的文字,也可以是一段描述原因或数据的文字。

数据库结构

报告节点结构

  • 路径为 reports/$reportKey
    • $reportKey 是节点数据键。
      • reportee 字段:负责数据的用户的 UID。
      • reporter 字段:创建报告的用户的 UID。通常是登录用户的 UID。
      • path 字段:数据的路径。它可以是实时数据库的路径或 Firestore 文档的路径。
      • type 字段:数据的类型。它可以是 userpostcommentphotochat 等。
      • reason 字段:报告的原因。它可以是 spamabusiveother
      • summary 字段:报告的摘要。
      • createdAt 字段:报告创建的时间。

报告列表节点结构

  • 路径为 reports/---key-list/$reportKey
    • ---key-list 是节点数据键。它保存所有的报告键。这是为了管理员的报告列表目的。
      • $reportKey 字段:这个键是报告键。它有 UID 作为值。

如何使用

初始化

你可以选择在使用前初始化 ReportService

通过 init 方法,你可以自定义 onCreate 回调。onCreate 回调在报告创建后被调用,它包含了新创建的 report 信息。

ReportService.instance.init();

显示报告按钮

你可以像这样显示报告按钮,并调用 report 方法。

TextButton(
  onPressed: () async {
    await ReportService.instance.report(
      context: context,
      otherUid: user.uid,
      documentReference: user.ref,
    );
  },
  child: const Text('报告'),
),

显示被封禁用户列表

你可以显示被封禁用户的列表。

ElevatedButton(
  onPressed: () =>
      ReportService.instance.showReportListScreen(context),
  child: const Text(
    '报告列表',
  ),
),

UI/UX 自定义

这是一个开源项目。你可以直接打开包的源码并复制/粘贴/编辑代码。代码应该足够简单以便重用。

  • 若要显示登录用户所作的报告,调用 ReportService.instance.showReportListScreen()
  • 若要自定义报告列表屏幕的 UI,你可以创建自己的屏幕并使用 ReportListView
    • ReportListView 支持大多数列表视图小部件的属性。

onCreate 回调

onCreate 是报告创建后的回调,它包含新创建的 report 信息。

例如,我们可以在报告创建后发送推送通知到管理员。

ReportService.instance.init(
  onCreate: (Report report) async {
    /// 设置推送通知。例如,向报告者发送推送通知
    /// 或者开发者可以向管理员发送推送通知
    MessagingService.instance.sendMessageToUids(
      uids: [report.reportee],
      title: '你已被举报',
      body: '举报原因 ${report.reason}',
      data: {
        "action": 'report',
        'reportId': report.id,
        'documentReference': report.documentReference.toString(),
      },
    );
  },
);

示例代码

import 'package:easyuser/easyuser.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:easy_report/easy_report.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  print('firebase project id: ${FirebaseDatabase.instance.app.options.projectId}');
  runApp(const EasyReportApp());
}

class EasyReportApp extends StatelessWidget {
  const EasyReportApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({
    super.key,
  });

  [@override](/user/override)
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  [@override](/user/override)
  void initState() {
    super.initState();
    init();
  }

  init() async {
    ReportService.instance.init();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          AuthStateChanges(
            builder: (user) {
              return user == null
                  ? const EmailPasswordLogin()
                  : Column(
                      children: [
                        Text('欢迎用户 uid: ${user.uid}'),
                        ElevatedButton(
                          onPressed: () {
                            UserService.instance.signOut();
                          },
                          child: const Text('退出登录'),
                        ),
                      ],
                    );
            },
          ),
          ElevatedButton(
            onPressed: () {
              ReportService.instance.report(
                context: context,
                reportee: 'reportee',
                type: 'type',
                path: 'user-uid',
                summary: 'summary',
              );
            },
            child: const Text('报告一个用户'),
          ),
          ElevatedButton(
            onPressed: () {
              ReportService.instance.report(
                context: context,
                reportee: 'u2',
                type: 'post',
                path: 'post 1',
                summary: 'summary 1',
              );
            },
            child: const Text('报告一篇帖子'),
          ),
          ElevatedButton(
            onPressed: () {
              ReportService.instance.report(
                context: context,
                reportee: 'u3',
                type: 'comment',
                path: 'comment 1',
                summary: '我举报这条评论 3',
              );
            },
            child: const Text('报告一条评论'),
          ),
          ElevatedButton(
            onPressed: () {
              ReportService.instance.report(
                context: context,
                reportee: 'uid of the user: if it is a chat room, report one of the master',
                type: 'chat',
                summary: '举报聊天室',
                path: '/chat/room/chat_room_id',
              );
            },
            child: const Text('举报一个聊天室'),
          ),
          AuthStateChanges(
            builder: (user) => user == null
                ? const SizedBox.shrink()
                : const Expanded(
                    child: ReportListView(),
                  ),
          ),
        ],
      ),
    );
  }
}

更多关于Flutter数据报告插件easy_report的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter数据报告插件easy_report的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


easy_report 是一个用于 Flutter 应用的数据报告插件,它可以帮助开发者轻松地收集和报告应用中的各种数据,如用户行为、错误日志、性能指标等。以下是如何使用 easy_report 插件的详细步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 easy_report 依赖:

dependencies:
  flutter:
    sdk: flutter
  easy_report: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖包。

2. 初始化插件

main.dart 文件中初始化 easy_report 插件。通常,你需要在 runApp 之前进行初始化:

import 'package:flutter/material.dart';
import 'package:easy_report/easy_report.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化EasyReport
  await EasyReport.init(
    apiKey: 'YOUR_API_KEY',  // 替换为你的API Key
    appVersion: '1.0.0',     // 你的应用版本号
    enableDebugLogging: true, // 是否启用调试日志
  );

  runApp(MyApp());
}

3. 报告数据

你可以使用 EasyReport 来报告各种数据,例如用户行为、错误日志、性能指标等。

报告用户行为

EasyReport.logEvent(
  eventName: 'button_click',
  parameters: {'button_id': 'submit_button'},
);

报告错误日志

try {
  // 你的代码
} catch (e, stackTrace) {
  EasyReport.logError(
    error: e,
    stackTrace: stackTrace,
    context: 'Error in submit form',
  );
}

报告性能指标

EasyReport.logPerformance(
  metricName: 'load_time',
  value: 1200, // 1200毫秒
  unit: 'ms',
);

4. 自定义配置

你可以根据需要对 EasyReport 进行自定义配置:

await EasyReport.init(
  apiKey: 'YOUR_API_KEY',
  appVersion: '1.0.0',
  enableDebugLogging: true,
  reportInterval: Duration(minutes: 5), // 设置报告间隔时间
  maxEventsPerBatch: 50, // 每批最多报告的事件数量
);

5. 手动提交报告

如果你需要手动提交报告,可以调用 EasyReport.flush()

await EasyReport.flush();

6. 处理用户隐私

easy_report 提供了选项来处理用户隐私问题。你可以选择禁用数据收集:

EasyReport.setEnabled(false); // 禁用数据收集

7. 调试和日志

启用调试日志可以帮助你查看 easy_report 的内部操作:

await EasyReport.init(
  apiKey: 'YOUR_API_KEY',
  appVersion: '1.0.0',
  enableDebugLogging: true,
);

8. 处理报告结果

你可以监听报告结果并进行相应处理:

EasyReport.onReportSuccess.listen((event) {
  print('Report成功: $event');
});

EasyReport.onReportFailure.listen((error) {
  print('Report失败: $error');
});

9. 集成后端服务

easy_report 通常需要与后端服务进行集成,以便将收集到的数据发送到服务器。你需要确保后端服务能够处理这些数据,并生成相应的报告。

10. 示例代码

以下是一个完整的示例代码,展示了如何使用 easy_report 插件:

import 'package:flutter/material.dart';
import 'package:easy_report/easy_report.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await EasyReport.init(
    apiKey: 'YOUR_API_KEY',
    appVersion: '1.0.0',
    enableDebugLogging: true,
  );

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('EasyReport Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              EasyReport.logEvent(
                eventName: 'button_click',
                parameters: {'button_id': 'submit_button'},
              );
            },
            child: Text('Click Me'),
          ),
        ),
      ),
    );
  }
}
回到顶部