![State.initState() must be a void method without an async
keyword. how can i solve this probelms]1
@override
Future<void> initState() async {
// TODO: implement initState
super.initState();
_current_location();
BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5),
'assets/fff.png').then((onValue) {
pinLocationIcon = onValue;
});
//createCustomMarker(context);
// final Marker marker = Marker(icon: BitmapDescriptor.fromBytes(markerIcon));
DatabaseReference ref = FirebaseDatabase.instance.reference();
ref.child('users').once().then((DataSnapshot snapshot) {
Map<dynamic, dynamic> values = snapshot.value;
print(values.toString());
values.forEach((k, v) {
allMarkers.add(Marker(
markerId: MarkerId(k),
draggable: false,
icon: pinLocationIcon,
position: LatLng(v["latitude"], v["longitude"]),
infoWindow: InfoWindow(title: v["name"]),
onTap: () {
_onMarkerTapped(v["name"]);
},
),);
});
});
}
1条答案
按热度按时间aor9mmx11#
initState
* 必须 * 是一个不带参数并返回void
的方法。这是因为它覆盖了超类中同名的方法(StatelessWidget
或State<StatefulWidgetType>
。因此,该限制是固定和绑定的约定;你无法改变它。当然,这也意味着
initState
不能被标记为async
,因为任何标记为async
的方法都会隐式返回一个Future
,但是如果该方法返回任何东西,它就不能有一个void
的返回类型,这违反了重写契约。如果你需要从
initState
中调用一个async
方法,你可以通过不调用await
来实现:然而,如果你的小部件需要
async
方法中的数据,你不能简单地等待Future
返回,然后再构建小部件。Flutter不允许这样做,因为不知道Future
需要多长时间才能返回,并且在此之前停止构建小部件可能会阻塞你的整个应用。相反,你需要让你的小部件正常构建,然后有一种方法来通知你的小部件在
Future
返回时更新,这在FutureBuilder
中最容易做到:initState
调用async
方法,而是从FutureBuilder
调用它。)*编辑:如前所述,这种方法只适用于OP的情况,即等待的future最终总是返回一个值。但情况并不总是如此--有时future根本不返回值,只是一个长时间运行的进程。有时future可能返回null而不是具体的数据。有时future可能导致错误而不是成功完成。在这些情况下,
snapshot.data
在future完成之后将为空,在这种情况下snapshot.hasData
将总是为假。在这些情况下,您可以使用
snapshot.connectionState
来监视将来的状态,而不是依赖于snapshot.hasData
来等待数据出现: