当应用程序在 Firebase 测试实验室上运行时,我想在 Flutter 项目中禁用 Firebase Analytics 的日志记录。根据 Firebase 文档,可以通过在 MainActivity.java 中添加以下内容来检测 TestLab
String testLabSetting = Settings.System.getString(getContentResolver(), "firebase.test.lab");
if ("true".equals(testLabSetting)) {
// Do something when running in Test Lab
// ...
}
如何在 main.dart 的 dart 端访问此测试的结果,这是我要禁用日志记录的地方(因为在 dart 代码中已经禁用了日志记录还有其他一些原因)。
谢谢!
最佳答案
我刚发现这个。我还没有尝试过:
https://pub.dev/packages/flutter_runtime_env
This project allows you to check if you're running in the Firebase Test Labs
你可以像他们的例子一样使用它:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_runtime_env/flutter_runtime_env.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _shouldBeEnabled = false;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
var result = await shouldEnableAnalytics();
setState(() {
_shouldBeEnabled = result;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Should Enable Analytics'),
),
body: Center(
child: Text('Should Analytics be Enabled: $_shouldBeEnabled\n'),
),
),
);
}
}
编辑:我想我找到了更好的解决方案。
https://pub.dev/packages/flutter_sentry
它有以下方法
/// Return `true` if running under Firebase Test Lab (includes pre-launch
/// report environment) on Android, `false` otherwise.
static Future<bool> isFirebaseTestLab() async
这似乎是迄今为止最好的解决方案......编辑2:
去他妈的!我刚刚创建了一个小插件。
https://pub.dev/packages/is_firebase_test_lab_activated
关于android - 从 Flutter Dart 中的 MainActivity.java 读取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59432805/