【发布时间】:2021-10-23 11:01:39
【问题描述】:
我正在尝试根据用户在 Firebase 中的“活动”状态在地图小部件上放置和删除标记:
Map <MarkerId, Marker> markers = <MarkerId, Marker>{};
.
.
.
markerStream.listen((List<DocumentSnapshot> documentList) {
documentList.forEach((DocumentSnapshot document) {
bool activeField = (document.data() as dynamic)['active'];
final GeoPoint point = (document
.data() as dynamic)['position']['geopoint'];
final uid = (document.data() as dynamic)['uid'].toString();
//if field 'active' in firebase is set to true or false
activeField == true ? _addMarker(point.latitude, point.longitude, uid)
: _removeMarker(uid);
});
}
删除方法:
_removeMarker( String uid) {
// checks uid is in markers.values map, if there, remove from collection , if not,
//I need a way to put continue or ignore in the `orElse` parameter instead of null, because the markerId cannot be null:
Marker? _markers = markers.values.firstWhere(
(thisMarker) => thisMarker.markerId.value == uid ,
orElse: () => null);
setState(() {
markers.remove(_marker);
});
}
当我省略 orElse 参数时,此代码运行。如果 firebase 中的所有条目在运行时都设置为 true,则会出现标记,并且在切换为 false 时会消失。问题是当条目在运行之前已经处于“假”状态时,会导致错误元素错误,因为它仍未存储在集合中。或者从 true 切换回 false 不会从地图小部件中删除标记。
【问题讨论】: