firebase / firebase/firebase-tools

Realtime Database connection is lost when app is disconnected from WiFi and reconnected back again

Open
#6,507 5 comments 0 reactions 0 assignees View on GitHub
emulators: database reproducible type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info

12.8.0

**firebase-tools:**

macOs

**Platform:**

### [REQUIRED] Test case

I have a Flutter app that is running on a real Android device with the Firebase emulator. I followed [documentation](https://firebase.google.com/docs/database/flutter/offline-capabilities) to detect user presence. Here is the code from the docs:

```
final connectedRef = FirebaseDatabase.instance.ref(".info/connected");
connectedRef.onValue.listen((event) {
final connected = event.snapshot.value as bool? ?? false;
if (connected) {
debugPrint("Connected.");
} else {
debugPrint("Not connected.");
}
});
```

The problem starts when I disconnect the app from WiFi and then reconnect. I noticed that I lost the connection to the Realtime Database even though I was connected to WiFi. I also noticed that since I no longer have the Realtime Database connection, the document I am interested in has the right data in the app memory but not in the Realtime Database emulator.

This is how I am listening to the document that I am interested in:

```
FirebaseDatabase.instance.ref('users/$uid').onValue.listen((event) {
debugPrint('data: ${event.snapshot.value}');
});
```

### [REQUIRED] Steps to reproduce

See above

### [REQUIRED] Expected behavior
I expect the Realtime Database connection to come back after reconnecting to WiFi.

I also noticed that when I use the following command, the app crashes even though the command is executed once.

```
FirebaseDatabase.instance.setPersistenceEnabled(true);
```

### [REQUIRED] Actual behavior
See above

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.