flutter中引入高德地图报错【../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/amap_map_fluttify-2.0.2/lib/src/facade/amap_view.widget.dart:282:29: Error: Method 'addPostFrameCallback' cannot be called on 'WidgetsBinding?' because it is potentially null. - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../flutter/flutter/packages/flutter/lib/src/widgets/binding.dart'). Try calling using ?. instead. WidgetsBinding.instance.addPostFrameCallback((duration) async { ^^^^^^^^^^^^^^^^^^^^】
时间: 2023-07-01 12:22:08 浏览: 625
基于uniapp、flutter、vue构建的多平台校园社区(支持云部署)源码.zip
这个报错是因为在Flutter 2.0及以上版本中,WidgetsBinding.instance被标记为可选参数,即可能为空。解决这个问题的方法是使用“?.”操作符代替“.”操作符调用WidgetsBinding实例中的方法。你可以在addPostFrameCallback方法前加上“WidgetsBinding.instance?”来解决这个问题,代码如下所示:
```
WidgetsBinding.instance?.addPostFrameCallback((duration) async {
// your code here
});
```
这样可以避免WidgetsBinding实例为空时引发的异常。
阅读全文