Flutter Firebase实时数据库监听值更改多次保存新值和相同值

mklgxw1f  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(121)

我正在监听字符串更改的值,以便将其保存到本地数据库。它会多次触发保存数据。对于同一个值。是否有任何方法可以在更改时循环一次...

locationListener() async {
   emit(LoadingListentLocState());
   String? latLong;
   final loc = FirebaseDatabase.instance.ref().child('h/location');

   emit(LoadingListentLocState());

   loc.onValue.listen((DatabaseEvent event) {

   latLong = event.snapshot.value.toString();

   CacheHelper.saveData(key: "latLong", value: latLong);
   locationsState = true;
   locationUpdated = true;
   getMapStatus = true;
   mapsStateMsg = MapStatesMsg(MapStates.LASTUPDATED);
   loadingWebViewMap(latLong);

    savingLocationToDB(latLong);

   emit(UpdatedListentLocState());
 });

}

iqjalb3h

iqjalb3h1#

您可能会寻找“独特":

late StreamSubscription sub;
Stream<DatabaseEvent> stream = newClientRef.onValue;
sub = stream.distinct().listen(
(DatabaseEvent event) async {.. save ..}

Distinct Description

相关问题