ft_mobile_agent_flutter 0.0.1-dev.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 39

Dataflux-SDK-flutter #

agent #

基于 ft ios android 调用的 plugin

使用 #

import 'package:ft_mobile_agent_flutter/ft_mobile_agent.dart';

1. 初始化配置 #

设置 Config 的属性 ,启动 SDK

  • 方法一
 static void configX(Config con)
  • 方法二
 /**
  * @method 指定初始化方法
  * @param metricsUrl     FT-GateWay metrics
  * @param akId           access key ID
  * @param akSecret       access key Secret
  * @param enableLog      是否打印日志
  * @param needBindUser   是否需要绑定用户
  * @param monitorType    监控类型
  * @param useGeoKey      是否使用高德作为地址解析器(该参数仅对 Android 平台有效)
  * @param geoKey         高德 key(该参数仅对 Android 平台有效)
  */
 static Future<void> config(String serverUrl,
      {String akId,
      String akSecret,
      String dataKitUUID,
      bool enableLog,
      bool needBindUser,
      int monitorType,
      bool useGeoKey,
      String geoKey}) async

2. 位置信息状态获取 #

/**
 * @method 位置信息状态获取 来判断位置信息是否获取成功
 * @param geoKey      使用高德作为地址解析器(该参数仅对 Android 平台有效)
 */
static Future<Map<dynamic,dynamic>> startLocation({String geoKey}) async
  • 返回值

    返回 Mapcode 表示错误码message 表示错误信息。 code = 0 : 位置信息获取成功。

3. 上报数据 与 上报列表 #

  • 上报数据
/**
 * 上报数据
 * @param measurement      当前数据点所属的指标集
 * @param tags             自定义标签
 * @param field            自定义指标
 */
static Future<Map<dynamic, dynamic>> track(
      String measurement, Map<String, dynamic> fields,
      [Map<String, dynamic> tags]) async
/**
 * 主动埋点,可多条上传。   立即上传 回调上传结果
 * @param trackList     主动埋点数据数组
 */
 static Future<Map<dynamic, dynamic>> trackList(
      List<TrackBean> list) async

  • 返回值

    返回 Mapcode 表示网络请求返回的返回码,response 为服务端返回的信息。 code 的值除了 HTTP 协议中规定的返回码, FT SDK 中额外规定了 4 种类型的 错误码,他们是 101,102,103,104,他们分别代表的意思是网络问题、参数问题、IO异常和未知错误。

4. 上报流程图 #

  • 方法
/**
 * 上报流程图
 * @param production   指标集 命名只能包含英文字母、数字、中划线和下划线,最长 40 个字符,区分大小写
 * @param traceId      标示一个流程单的唯一 ID
 * @param name         流程节点名称
 * @param duration     流程单在当前流程节点滞留时间或持续时间,毫秒为单位
 * @param parent       当前流程节点的上一个流程节点的名称,如果是流程的第一个节点,可不上报 (可选)
 * @param tags         自定义标签 (可选)
 * @param fields       自定义指标 (可选)
 */
static Future<void> trackFlowChart(
      String production, String traceId, String name, int duration,
      {String parent,
      Map<String, dynamic> tags,
      Map<String, dynamic> fields}) async

5. 主动埋点数据上报(后台运行) #

  • 方法
 /**
  * 追踪自定义事件。 存储数据库,等待上传
  * @param measurement      指标(必填)
  * @param field            指标值(必填)
  * @param tags             标签(选填)
  */
static Future<void> trackBackground(
      String measurement, Map<String, dynamic> fields,
      {Map<String, dynamic> tags}) async

6. 用户的绑定与注销 #

FT SDK 提供了绑定用户和注销用户的方法,Config 属性needBindUserYES 时(默认为 NO),用户绑定的状态下,才会进行数据的传输。

  • 绑定用户
 /**
  * 绑定用户信息
  * @param name     用户名
  * @param Id       用户Id
  * @param exts     用户其他信息
  */
  static Future<void> bindUser(String name, String id,
      {Map<String, dynamic> extras}) async
  • 解绑用户
 /**
  * 注销用户信息
  */
 static Future<void> unbindUser() async

7. 停止 SDK 后台正在执行的操作 #

  • 方法
 /**
  * 关闭 SDK 正在做的操作
  */
 static Future<void> stopSDK() async

8. 使用示例 #

方法使用示例

参数与错误码 #

1. Config 可配置参数 #

字段类型说明是否必须
metricsUrlStringFT-GateWay metrics 写入地址
akIdStringaccess key IDenableRequestSigning 为 true 时,必须要填
akSecretStringaccess key SecretenableRequestSigning 为 true 时,必须要填
enableLogbool设置是否允许打印日志否(默认NO)
monitorTypeint采集数据
needBindUserbool是否开启绑定用户数据否(默认NO)

monitorType 可设置:

 class MonitorType {
     static const int ALL = 1;
     static const int BATTERY  = 1 << 1;   // 电池总量、使用量
     static const int MEMORY   = 1 << 2;   // 内存总量、使用率
     static const int CPU      = 1 << 3;   // CPU型号、占用率
     static const int GPU      = 1 << 4;   // GPU型号、占用率
     static const int NETWORK  = 1 << 5;   // 网络的信号强度、网络速度、类型、代理
     static const int CAMERA   = 1 << 6;   // 前置/后置 像素
     static const int LOCATION = 1 << 7;   // 位置信息  eg:上海
  }    
  

2. TrackBean #

字段类型说明是否必须
measurementString当前数据点所属的指标集
tagsMap自定义标签
fieldsMap自定义指标

3. 错误码 #

字段说明
NetWorkException101网络问题
InvalidParamsException102参数问题
FileIOException103文件 IO 问题
UnknownException104未知问题

常见问题 #

0.0.1-dev.4 #

  • 优化地理位置获取的方法

0.0.1-dev.3 #

  • 文档格式修改

0.0.1-dev.2 #

  • 修改文档

0.0.1-dev.1 #

  • 用户自定义埋点
  • 流程图上报方法
  • 监控项目关联

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ft_mobile_agent_flutter/ft_mobile_agent_flutter.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeRoute(),
    );
  }
}

class HomeRoute extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<HomeRoute> {
  var permissions = [
    Permission.location,
    Permission.camera,
    Permission.storage,
    Permission.phone
  ];
  var locationState = "";
  var locationStateGeo = "";

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      requestPermission(permissions);
    } else if (Platform.isIOS) {
      requestPermission([Permission.location]);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              _buildConfigWidget(),
              _buildSyncImmediateWidget(),
              _buildSyncListImmediateWidget(),
              _buildSyncWidget(),
              _buildFlowChartWidget(),
              _buildBindUserWidget(),
              _buildUnBindUserWidget(),
              _buildStopSDKWidget(),
              _buildStartLocationWidget(),
              _buildGeoStartLocationWidget(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildConfigWidget() {
    return RaisedButton(
      child: Text("设置配置"),
      onPressed: () async {
        /// 配置方法一
        FTMobileAgentFlutter.configX(
            Config("http://10.100.64.106:19457/v1/write/metrics")
                .setAK("accid", "accsk")
                .setDataKit("flutter_datakit")
                .setEnableLog(true)
                .setNeedBindUser(false)
                .setGeoKey(true, "46f60b8b6963de515749001b92a866c0")
                .setMonitorType(MonitorType.BATTERY |
                    MonitorType.NETWORK |
                    MonitorType.LOCATION |
                    MonitorType.GPU));

        /// 配置方法二
        /**FTMobileAgentFlutter.config(
            "http://10.100.64.106:19457/v1/write/metrics",
            akId: "accid",
            akSecret: "accsk",
            dataKitUUID: "flutter_datakit",
            enableLog: true,
            needBindUser: false,
            monitorType: MonitorType.ALL);*/
      },
    );
  }

  Widget _buildSyncImmediateWidget() {
    return RaisedButton(
      child: Text("同步一条数据(直接上传)"),
      onPressed: () async {
        var result = await FTMobileAgentFlutter.track(
            "flutter_list_test", {"platform": "flutter"}, {"method": "直接同步"});
        print("request success: $result");
      },
    );
  }

  Widget _buildSyncListImmediateWidget() {
    return RaisedButton(
      child: Text("同步一组数据(直接上传)"),
      onPressed: () async {
        var result = await FTMobileAgentFlutter.trackList([
          TrackBean("flutter_list_test", {"platform": "flutter"}),
          TrackBean("flutter_list_test", {"platform": "flutter"},
              tags: {"method": "直接同步"}),
        ]);
        print("request success: $result");
      },
    );
  }

  Widget _buildSyncWidget() {
    return RaisedButton(
      child: Text("同步(后台执行)"),
      onPressed: () {
        FTMobileAgentFlutter.trackBackground(
            "flutter_list_test", {"method": "后台同步"},
            tags: {"platform": "flutter"});
      },
    );
  }

  Widget _buildFlowChartWidget() {
    return RaisedButton(
      child: Text("同步流程图数据"),
      onPressed: () {
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "开始", 1000);
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "流程一", 1000,
            parent: "开始");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "流程二", 1000,
            parent: "流程一");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "选择", 1000,
            parent: "流程二");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "流程三", 1000,
            parent: "选择");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "流程四", 1000,
            parent: "选择");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "流程五", 1000,
            parent: "流程三");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "流程五", 1000,
            parent: "流程四");
        FTMobileAgentFlutter.trackFlowChart(
            "flutter_agent", "trace-001", "结束", 1000,
            parent: "流程五");
      },
    );
  }

  Widget _buildBindUserWidget() {
    return RaisedButton(
      child: Text("绑定用户"),
      onPressed: () {
        FTMobileAgentFlutter.bindUser("flutter_demo", "id_001",
            extras: {"platform": "flutter"});
      },
    );
  }

  Widget _buildUnBindUserWidget() {
    return RaisedButton(
      child: Text("解绑用户"),
      onPressed: () {
        FTMobileAgentFlutter.unbindUser();
      },
    );
  }

  Widget _buildStopSDKWidget() {
    return RaisedButton(
      child: Text("停止正在执行的操作"),
      onPressed: () {
        FTMobileAgentFlutter.stopSDK();
      },
    );
  }

  Widget _buildStartLocationWidget() {
    return RaisedButton(
      child: Text("定位异步通知结果" + locationState),
      onPressed: () async {
        var result = await FTMobileAgentFlutter.startLocation();
        if (result != null) {
          setState(() {
            locationState =
                "-code:${result['code']},message:${result['message']}";
          });
        }
      },
    );
  }

  Widget _buildGeoStartLocationWidget() {
    return RaisedButton(
      child: Text("(仅 Android )高德定位异步通知结果" + locationStateGeo),
      onPressed: () async {
        var result = await FTMobileAgentFlutter.startLocation(
            geoKey: "46f60b8b6963de515749001b92a866c0");
        if (result != null) {
          setState(() {
            locationStateGeo =
                "-code:${result['code']},message:${result['message']}";
          });
        }
      },
    );
  }

  void _showPermissionTip(String tip) {
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text("警告"),
            content: Text("你拒绝了\n$tip 权限,拒绝后将无法使用"),
            actions: <Widget>[
              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop();
                  requestPermission(permissions);
                },
                child: Text("重新请求"),
              ),
              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: Text("拒绝"),
              )
            ],
          );
        });
  }

  Future<void> requestPermission(List<Permission> permission) async {
    final status = await permission.request();
    status.removeWhere((permission, state) => state.isGranted);
    var tip = "";
    if (status.isNotEmpty) {
      status.forEach((permission, state) {
        state.isUndetermined;
        tip += permission.toString() + "\n";
      });
      _showPermissionTip(tip);
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ft_mobile_agent_flutter: ^0.0.1-dev.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ft_mobile_agent_flutter/ft_mobile_agent_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
45
Overall:
Weighted score of the above. [more]
39
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Linux

Because:

  • package:ft_mobile_agent_flutter/ft_mobile_agent_flutter.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Web

Because:

  • package:ft_mobile_agent_flutter/ft_mobile_agent_flutter.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform Windows

Because:

  • package:ft_mobile_agent_flutter/ft_mobile_agent_flutter.dart that declares support for platforms: Android, iOS

Package does not support Flutter platform macOS

Because:

  • package:ft_mobile_agent_flutter/ft_mobile_agent_flutter.dart that declares support for platforms: Android, iOS

Package not compatible with SDK dart

Because:

  • ft_mobile_agent_flutter that is a package requiring null.

Health suggestions

Format lib/ft_mobile_agent_flutter.dart.

Run flutter format to format lib/ft_mobile_agent_flutter.dart.

Maintenance issues and suggestions

The description contains too many non-ASCII characters. (-20 points)

The site uses English as its primary language. The value of the description field in your package's pubspec.yaml field should primarily contain characters used in English.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test