Closed om-ha closed 2 years ago
Note: bug
label might not be appropriate here, apologies.
Looks like I got this to work:
StateNotifier Definition
class DataHolderStateNotifier<T> extends StateNotifier<T?> {
final String stringField;
final bool boolField;
DataHolderStateNotifier({
required this.stringField,
required this.boolField,
}) : super(null);
void set({T? newValue}) {
state = newValue;
}
}
final counterProvider =
StateNotifierProvider<DataHolderStateNotifier<int>, int?>((Ref ref) {
return DataHolderStateNotifier(
stringField: 'counter',
boolField: false,
);
});
final temperatureProvider =
StateNotifierProvider<DataHolderStateNotifier
- Provider Usage
```dart
// read
int? counterValue = ref.watch(counterProvider);
double? temperatureValue = ref.watch(temperatureProvider);
// write
ref.watch(counterProvider.notifier).set(newValue: 100);
ref.watch(temperatureProvider.notifier).set(newValue: 24.42);
I hope this proves useful to others.
Describe the bug Cannot extend
StateNotifier
from a generic classTo Reproduce
This works:
This does not work (does not compile):
Expected behavior To be able to use generic type with a class, and still extend
StateNotifier
.Problem I am trying to solve
DataHolder
as a part of Riverpod's provider (access throughRef
)T
insideDataHolder
Moving to
StateNotifier
implementation (attempted one above) toChangeNotifier
/ValueNotifier
implementation, see below: