qt_apm_sdk 2.6.0 copy "qt_apm_sdk: ^2.6.0" to clipboard
qt_apm_sdk: ^2.6.0 copied to clipboard

QuickTracking flutter APM SDK Plugin.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:qt_common_sdk/qt_common_sdk.dart';
import 'package:qt_apm_sdk/qt_apm_sdk.dart';

import './pages/exception.dart';
import './pages/lazy_load_page.dart';
import './pages/list.dart';
import './pages/white_screen.dart';

Map<String, WidgetBuilder> routes = {
  "/": (context) => HomePage(),
  "/exception": (context) => ExceptionPage(),
  "/white_screen": (context) => WhiteScreenPage(),
  "/list": (context) => ListPage(),
  "/lazy_load": (context) => ScrollLazyLoadPage(),
};

void main() {
  final QuickTrackingFlutterApmSdk qtApmSdk = QuickTrackingFlutterApmSdk(
    name: '', // 应用或模块名称
    bver: '', //应用或模块版本+构建号
    flutterVersion: '3.29.0', //Flutter SDK 版本
    engineVersion: 'f73bfc4522', //Flutter 引擎版本
    enableLog: true, //是否开启SDK日志打印 (默认关闭)
    enableTrackingPageFps: true, //开启监测页面帧率(默认关闭)
    enableTrackingPagePerf: true, //开启监测页面性能(默认关闭)
    errorFilter: { //设置采集的异常黑白名单
      "mode": "ignore",
      // "rules": [RegExp('RangeError')],
      "rules": [],
    },
    trackDomain: "", //收数域名,如果通过统计SDK配置收数域名,此处可不配置
    initFlutterBinding: MyApmWidgetsFlutterBinding.ensureInitialized,
    // onError: (exception, stack) {},
  );

  qtApmSdk.init(appRunner: (observer) async {
    // 确保去掉原有的WidgetsFlutterBinding.ensureInitialized() ,以免出现重复初始化绑定的异常造成无法正常初始化,
    // SDK内部已通过initFlutterBinding入参带入继承的WidgetsFlutterBinding实现初始化操作
    // 依赖ensureInitialized()初始化的代码可在此调用
    // 需要异步获取设置应用名称和版本号可在此回调中操作
    // SDK实例化的设置可先将name和bver 为 "",然后通过以下方式进行设置
    // 如:umengApmSdk.name = 'app_demo';
    qtApmSdk.name = 'app_demo';
    qtApmSdk.bver = '2.5.0';
    return MyApp(observer);
  });
}

class MyApmWidgetsFlutterBinding extends ApmWidgetsFlutterBinding {
  @override
  void handleAppLifecycleStateChanged(AppLifecycleState state) {
    // 添加自己的实现逻辑
    // print('AppLifecycleState changed to $state');
    super.handleAppLifecycleStateChanged(state);
  }

  static WidgetsBinding? ensureInitialized() {
    MyApmWidgetsFlutterBinding();
    return WidgetsBinding.instance;
  }
}

// ignore: must_be_immutable
class MyApp extends StatelessWidget {
  MyApp([this._navigatorObserver]);

  NavigatorObserver? _navigatorObserver;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      routes: routes,
      initialRoute: "/",
      navigatorObservers: <NavigatorObserver>[
        _navigatorObserver ?? ApmNavigatorObserver.singleInstance
      ],
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class CustomTransitionPage extends PageRouteBuilder {
  final Widget widget;

  CustomTransitionPage({required this.widget})
      : super(
          pageBuilder: (BuildContext context, Animation<double> animation,
              Animation<double> secondaryAnimation) {
            return widget;
          },
          transitionsBuilder: (BuildContext context,
              Animation<double> animation,
              Animation<double> secondaryAnimation,
              Widget child) {
            Animation<Offset> customAnimation = Tween<Offset>(
              begin: Offset(3.0, 0.0),
              end: Offset.zero,
            ).animate(animation);

            return SlideTransition(
              position: customAnimation,
              child: child,
            );
          },
        );
}

class _MyAppState extends State {
  @override
  void initState() {
    super.initState();
    QTCommonSdk.setCustomDomain("主收数域名", "副收数域名");
    QTCommonSdk.setLogEnabled(true);
    QTCommonSdk.initCommon("android应用appkey", "iOS应用标识", "发布渠道");

  }

  void runCustomException() {
    try {
      // 模拟数组越界错误
      List<String> numList = ['1', '2'];
      print(numList[5]);
    } catch (e) {
      ExceptionTrace.captureException(
          exception: Exception(e), extra: {"user": '123'});
    }
  }

  void _showDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("AlertDialog Title"),
          content: Text("AlertDialog Body"),
          actions: <Widget>[
            TextButton(
              child: Text("CANCEL"),
              onPressed: () => Navigator.of(context).pop(),
            ),
            TextButton(
              child: Text("OK"),
              onPressed: () => Navigator.of(context).pop(),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextButton(
                  child: Text('dart error page'),
                  onPressed: () async {
                    Navigator.pushNamed(context, '/exception');
                  }),
              TextButton(
                  child: Text('dart error new page'),
                  onPressed: () async {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => ExceptionPage()));
                  }),
              TextButton(
                  child: Text('Page Transition'),
                  onPressed: () async {
                    Navigator.of(context)
                        .push(CustomTransitionPage(widget: ExceptionPage()));
                  }),
              TextButton(
                  child: Text('dart error release page'),
                  onPressed: () async {
                    Navigator.of(context).pushReplacementNamed('/exception');
                  }),
              TextButton(
                child: Text("Show Dialog"),
                onPressed: _showDialog,
              ),
              TextButton(
                  child: Text("dart white screen exception"),
                  onPressed: () async {
                    Navigator.of(context).pushReplacementNamed('/white_screen');
                  }),
              TextButton(
                  child: Text("lazy loading text list"),
                  onPressed: () async {
                    Navigator.pushNamed(context, '/list');
                  }),
              TextButton(
                  child: Text("lazy loading picture list"),
                  onPressed: () async {
                    Navigator.pushNamed(context, '/lazy_load');
                  }),
              TextButton(
                  child: Text("捕获主动上报 exception"),
                  onPressed: () {
                    runCustomException();
                  }),
            ]),
      ),
    );
  }
}
2
likes
120
points
54
downloads

Publisher

verified publisherpub-google.aplus.emas-poc.com

Weekly Downloads

QuickTracking flutter APM SDK Plugin.

Homepage

Documentation

API reference

License

GPL-3.0 (license)

Dependencies

flutter, http

More

Packages that depend on qt_apm_sdk