当前位置:   article > 正文

flutter 断言_flutter assert

flutter assert
assert 方法的参数可以为任何返回布尔值的表达式或者方法。 
如果返回的值为 true, 断言执行通过,执行结束。 如果返回值为 false,
断言执行失败,会抛出一个AssertionError异常 。

断言只在检查模式下运行有效,如果在生产模式运行,则断言不会执行。

assert(...)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

代码示例:

import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

extension SharedPreferencesExtension on SharedPreferences {
  Future<bool> setJson(String key, Map<String, dynamic> json) {
    assert(json != null);
    assert(key != null);

    var value = jsonEncode(json);
    return this.setString(key, value);
  }

  Map<String, dynamic> getJson(String key) {
    assert(key != null);

    var value = this.getString(key);
    var json = jsonDecode(value);
    return json;
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/455524
推荐阅读
相关标签
  

闽ICP备14008679号