简体   繁体   中英

firebase realtime database updating value twice

I am trying to set a bool value in real time database in flutter but it changes the value twice. first its set the value to false from true then it immediately change it to true again.

Future<void> changeTurnkey1(String roomCode, String key1) async {
var codeRef = _codeRef(roomCode);
await codeRef.child(playersKey).child(key1).get().then((value) async {
final Map snapshotMap = value.value;
bool val = snapshotMap[canPlay];
if (val) {
  await codeRef.child(playersKey).child(key1).update({
    canPlay: false,
  });
} else {
  codeRef.child(playersKey).child(key1).update({
    canPlay: true,
  });
}
});
}

Seems you're using both async/await and .then() ... This would definitely have undesired effects.

Try just using async/await like this:

Future<void> changeTurnkey1(String roomCode, String key1) async {
var codeRef = _codeRef(roomCode);
var value = await codeRef.child(playersKey).child(key1).get()
final Map snapshotMap = value.value;
bool val = snapshotMap[canPlay];
if (val) {
  await codeRef.child(playersKey).child(key1).update({
    canPlay: false,
  });
} else {
  codeRef.child(playersKey).child(key1).update({
    canPlay: true,
  });
}
}

I have the same problem. Finally found out that our old server program(nodejs docker) forgot to stop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM