avoid_returning_null
Avoid returning null from members whose return type is bool, double, int, or num.
此规则已从最新的 Dart 版本中移除。
详情
#NOTE: This rule is removed in Dart 3.3.0; it is no longer functional.
AVOID returning null from members whose return type is bool, double, int, or num.
Functions that return primitive types such as bool, double, int, and num are generally expected to return non-nullable values. Thus, returning null where a primitive type was expected can lead to runtime exceptions.
BAD:
dart
bool getBool() => null;
num getNum() => null;
int getInt() => null;
double getDouble() => null;
GOOD:
dart
bool getBool() => false;
num getNum() => -1;
int getInt() => -1;
double getDouble() => -1.0;
使用方法
#要启用 avoid_returning_null
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 avoid_returning_null
:
analysis_options.yaml
yaml
linter:
rules:
- avoid_returning_null
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.