Flutter数据隐藏插件april_value_hide的使用

Flutter数据隐藏插件april_value_hide的使用

开始使用

在项目终端运行以下命令以激活此工具:

flutter pub global activate april_value_hide

使用方法

第一步:在 pubspec.yaml 中添加配置

在你的项目的 pubspec.yaml 文件中,添加以下内容:

dependencies:
  april_value_hide:
    jsons_dir: 'example/jsons'
    generated_file_dir: 'example'
    generated_file_name: 'HiddenValues'
    offset: '-999'

第二步:准备 JSON 文件

example/jsons 目录下创建一个名为 Values1.json 的文件,内容如下:

{
  "@desc": "Descriptions of the Dart class who will be generated.",
  "intValueKey": 999,
  "@intValueKey": "Descriptions of the int value.",
  "doubleValueKey": 123.123,
  "@doubleValueKey": "Descriptions of the double value.",
  "stringValueKey": "The value is a String.",
  "@stringValueKey": "Descriptions of the String value."
}

同时,在同一个目录下创建另一个名为 Values2.json 的文件,内容如下:

{
  "intValueKey": 8888
}

第三步:生成 Dart 文件

在项目终端运行以下命令以生成 Dart 文件:

flutter pub global run april_value_hide:generate

结果

生成的文件路径为 example/HiddenValues.dart,其内容如下:

// generated code, do not change by yourself
import 'dart:convert';

String _fromIntList(List<int> value) {
  return utf8.decode(
    value
        .map<int>((e) => e + (-999)) // 添加偏移值
        .map<int>((e) => ~e) // 按位取反
        .toList(),
  );
}

/// Descriptions of the Dart class who will be generated.
class Values1 {
  Values1._();

  /// Descriptions of the int value.
  /// >>>999<<<
  static final int intValueKey = int.parse(_fromIntList(const [941, 941, 941]));

  /// Descriptions of the double value.
  /// >>>123.123<<<
  static final double doubleValueKey = double.parse(_fromIntList(const [949, 948, 947, 952, 949, 948, 947]));

  /// Descriptions of the String value.
  /// >>>The value is a String.<<<
  static final String stringValueKey = _fromIntList(const [914, 894, 897, 966, 880, 901, 890, 881, 897, 966, 893, 883, 966, 901, 966, 915, 882, 884, 893, 888, 895, 952]);
}

class Values2 {
  Values2._();

  /// >>>8888<<<
  static final int intValueKey = int.parse(_fromIntList(const [942, 942, 942, 942]));
}

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

1 回复

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


april_value_hide 是一个用于在 Flutter 应用中隐藏敏感数据的插件。它可以帮助你在调试或日志输出时隐藏敏感信息,例如 API 密钥、用户凭证等。使用这个插件可以防止敏感数据意外泄露。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 april_value_hide 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  april_value_hide: ^1.0.0  # 请使用最新的版本号

然后,运行 flutter pub get 来安装依赖。

使用插件

1. 导入包

在你的 Dart 文件中导入 april_value_hide 包:

import 'package:april_value_hide/april_value_hide.dart';

2. 隐藏敏感数据

你可以使用 ValueHider 类来隐藏敏感数据。以下是一个简单的示例:

void main() {
  String apiKey = "your_sensitive_api_key";
  
  // 创建一个 ValueHider 实例
  ValueHider hider = ValueHider(apiKey);
  
  // 使用 hiddenValue 获取隐藏后的值
  print("Original API Key: $apiKey");
  print("Hidden API Key: ${hider.hiddenValue}");
  
  // 你也可以自定义隐藏符号和隐藏长度
  ValueHider customHider = ValueHider(apiKey, hideSymbol: '*', hideLength: 5);
  print("Custom Hidden API Key: ${customHider.hiddenValue}");
}

3. 输出结果

运行上述代码后,你会看到类似如下的输出:

Original API Key: your_sensitive_api_key
Hidden API Key: ****************
Custom Hidden API Key: *****
回到顶部