虽然这可行,但最好使用常量
kReleaseMode或
kDebugMode。有关完整说明,请参见下面的Rémi答案,这可能是公认的问题。
最简单的方法是使用
assert它,因为它仅在调试模式下运行。
这是Flutter的Navigator源代码中的一个示例:
assert(() { if (navigator == null && !nullOk) { throw new FlutterError( 'Navigator operation requested with a context that does not include a Navigator.n' 'The context used to push or pop routes from the Navigator must be that of a ' 'widget that is a descendant of a Navigator widget.' ); } return true;}());特别要注意的是,
()在调用结束时-assert只能对布尔值进行操作,因此仅传入函数是行不通的。



