You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中Firebase Performance自定义URL监控无数据问题排查

Flutter Firebase性能监控自定义Dio请求无数据的解决步骤

你确实遗漏了代码层的请求拦截配置和原生平台的基础配置,以下是完整的补全步骤:


1. 初始化Firebase核心服务

Firebase所有功能都依赖核心初始化,必须在App启动时执行:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 初始化Firebase核心
  await Firebase.initializeApp();
  runApp(MyApp());
}

2. 给Dio添加Firebase性能监控拦截器

Firebase Performance不会自动捕获Dio的请求,需要手动实现拦截器来统计请求指标:

import 'package:dio/dio.dart';
import 'package:firebase_performance/firebase_performance.dart';

class DioPerformanceInterceptor extends Interceptor {
  final FirebasePerformance _perf = FirebasePerformance.instance;

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    // 创建请求指标实例
    final metric = _perf.newHttpMetric(options.uri.toString(), HttpMethod.get);
    await metric.start();
    // 将指标存入请求参数,后续响应/错误时复用
    options.extra['httpMetric'] = metric;
    handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) async {
    final metric = response.requestOptions.extra['httpMetric'] as HttpMetric?;
    if (metric != null) {
      metric.responseCode = response.statusCode;
      metric.responsePayloadSize = response.data?.toString().length ?? 0;
      await metric.stop();
    }
    handler.next(response);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) async {
    final metric = err.requestOptions.extra['httpMetric'] as HttpMetric?;
    if (metric != null) {
      metric.responseCode = err.response?.statusCode ?? -1;
      await metric.stop();
    }
    handler.next(err);
  }
}

然后给你的Dio实例绑定拦截器:

final dio = Dio()..interceptors.add(DioPerformanceInterceptor());

3. 原生平台配置补全

Android端(AndroidManifest.xml)

  • 确保添加必要权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 启用性能监控收集:
<application>
  <meta-data
      android:name="firebase_performance_collection_enabled"
      android:value="true" />
</application>

iOS端(Info.plist)

  • 启用性能监控:
<key>FirebasePerformanceCollectionEnabled</key>
<true/>
  • 若后端为HTTP协议(测试环境),需添加ATS例外(生产环境建议仅针对特定域名配置):
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

4. 控制台配置优化

  • 建议用通配符匹配URL,比如将api.myCompany.id/api/blueray/customer/profile改为api.myCompany.id/api/blueray/**,避免精确路径匹配导致漏统计;
  • 测试阶段可以先查看「所有网络请求」,确认自定义请求是否被捕获,再切换到自定义规则筛选。

5. 测试验证

  • 本地调试时调用几次目标接口,确保设备联网;
  • 可开启Debug模式加速数据上报:
    • Android:执行命令 adb shell setprop debug.firebase.performance.app <你的App包名>
    • iOS:在Xcode启动参数中添加 -FIRPerformanceDebugEnabled

内容的提问来源于stack exchange,提问作者Agung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 09:18:24