canonical / canonical/nm.dart

Empty property change streams

Open
#52 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
30
Forks
12
PR merge metrics
No merged PRs in 30d

Description

Would it make sense to output a warning, for example, if `NetworkManagerClient.propertiesChanged` or other similar properties are accessed before `NetworkManagerClient.connect()` has completed?

```dart
final client = NetworkManagerClient();
await client.connect();
client.propertiesChanged.listen((properties) => print('changed: $properties'));
await client.setSomething(...); // "changed: [...]"
}
```
vs.
```dart
final client = NetworkManagerClient();
client.propertiesChanged.listen((properties) => print('changed: $properties'));
await client.connect();
await client.setSomething(...); //
```

The above example may look silly because the order makes perfect sense and the fix is just a matter of swapping two lines. However, when the NM client is injected into a widget tree and lazily initialized when needed, things may get confusing when property change notifications go quietly missing when accidentally setting up subscriptions at construction time.

When the code is changed to something like this, the problem is no longer that obvious.
```dart
class NetworkViewModel extends ChangeNotifier {
NetworkViewModel(this.client) {
client.propertiesChanged.listen((_) => notifyListeners());
}
final NetworkManagerClient client;
bool get wirelessEnabled => client.wirelessEnabled;
Future init() => client.connect().then((_) => notifyListeners());
}

class NetworkViewState extends State {
void initState() {
super.initState();
context.read().init();
}
}
```

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.