Flutter app开发:错误捕获的正确姿势

2019-10-1418:36:51APP与小程序开发Comments2,309 views字数 3852阅读模式

软件开发过程中,错误和异常总是在所难免。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

不管是客户端的逻辑错误导致的,还是服务器的数据问题导致的,只要出现了异常,我们都需要一个机制来通知我们去处理。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

在 APP 的开发过程中,我们通过一些第三方的平台,比如 Fabric、Bugly 等可以实现异常的日志上报。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

Flutter 也有一些第三方的平台,比如 Sentry 可以实现异常的日志上报。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

但是为了更加通用一些,本篇不具体讲解配合某个第三方平台的异常日志捕获,我们会告知大家如何在 Flutter 里面捕获异常。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

至于具体的上报途径,不管是上报到自家的后台服务器,还是通过第三方的 SDK API 接口进行异常上报,都是可以的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

Demo 初始状态

首先我们新建 Flutter 项目,修改 main.dart 代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Flutter Crash Capture'),),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
复制代码

效果如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

Flutter app开发:错误捕获的正确姿势

捕获错误

我们修改 MyHomePage,添加一个 List 然后进行越界访问,改动部分代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

class MyHomePage extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   List<String> numList = ['1', '2'];
   print(numList[6]);
   return Container();
 }
}
复制代码

可以看到控制台报错如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following RangeError was thrown building MyHomePage(dirty):
flutter: RangeError (index): Invalid value: Not in range 0..1, inclusive: 6
复制代码

当然这些错误信息在界面上也有显示(debug 模式)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

那么我们如何捕获呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

其实很简单,有个通用模板,模板为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

import 'dart:async';

import 'package:flutter/material.dart';

Future<Null> main() async {
  FlutterError.onError = (FlutterErrorDetails details) async {
    Zone.current.handleUncaughtError(details.exception, details.stack);
  };

  runZoned<Future<void>>(() async {
    runApp(MyApp());
  },  onError: (error, stackTrace) async {
    await _reportError(error, stackTrace);
  });
}

Future<Null> _reportError(dynamic error, dynamic stackTrace) async {
  // TODO
}
复制代码

TODO 里面就可以执行埋点上报操作或者其他处理了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

完整例子如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

import 'dart:async';

import 'package:flutter/material.dart';

Future<Null> main() async {
  FlutterError.onError = (FlutterErrorDetails details) async {
    Zone.current.handleUncaughtError(details.exception, details.stack);
  };

  runZoned<Future<void>>(() async {
    runApp(MyApp());
  },  onError: (error, stackTrace) async {
    await _reportError(error, stackTrace);
  });
}

Future<Null> _reportError(dynamic error, dynamic stackTrace) async {
  print('catch error='+error);
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Flutter Crash Capture'),),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<String> numList = ['1', '2'];
    print(numList[6]);
    return Container();
  }
}
复制代码

运行可以看到控制台捕获到错误如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

flutter: catch error=RangeError (index): Invalid value: Not in range 0..1, inclusive: 6
复制代码

assert 妙用

我们知道,一般错误上报都是在打包发布到市场后才需要。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

平时调试的时候如果遇到错误,我们是会定位问题并修复的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

因此在 debug 模式下,我们不希望上报错误,而是希望直接打印到控制台。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

那么,这个时候就需要一种方式来区分现在是 debug 模式还是 release 模式,怎么区分呢?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

这个时候就需要用到 assert 了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

bool get isInDebugMode {
  // Assume you're in production mode.
  bool inDebugMode = false;

  // Assert expressions are only evaluated during development. They are ignored
  // in production. Therefore, this code only sets `inDebugMode` to true
  // in a development environment.
  assert(inDebugMode = true);

  return inDebugMode;
}
复制代码

从注释也可以知道,assert 表达式只在开发环境下会起作用,在生产环境下会被忽略。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

因此利用这一个,我们就可以实现我们的需求。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

上面的结论要验证也很简单,我们就不演示了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

完整模板

import 'dart:async';

import 'package:flutter/material.dart';

Future<Null> main() async {
  FlutterError.onError = (FlutterErrorDetails details) async {
    if (isInDebugMode) {
      FlutterError.dumpErrorToConsole(details);
    } else {
      Zone.current.handleUncaughtError(details.exception, details.stack);
    }
  };

  runZoned<Future<void>>(() async {
    runApp(MyApp());
  },  onError: (error, stackTrace) async {
    await _reportError(error, stackTrace);
  });
}

Future<Null> _reportError(dynamic error, dynamic stackTrace) async {
  // TODO
}

bool get isInDebugMode {
  // Assume you're in production mode.
  bool inDebugMode = false;

  // Assert expressions are only evaluated during development. They are ignored
  // in production. Therefore, this code only sets `inDebugMode` to true
  // in a development environment.
  assert(inDebugMode = true);

  return inDebugMode;
}
复制代码

debug 模式下,直接将错误打印到控制台,方便定位问题。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

release 模式下,将错误信息收集起来,上传到服务器。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

作者:安卓小煜
来源:掘金文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/16850.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/xcx/16850.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定