Refs need references to their instances
I am currently looking into this package as a way of finding a state managment solution that is of greater convenience than what has come before.
Notably, I am a enjoyer of Provider, but not of Riverpod.
In the current implementation of the package, to create a easily accessible value in a context,
- a Ref must be instantiated,
- then bound to a context,
- and finally accessed via a variable of that Ref instance.
Immediately this reminds me of Riverpod instead of Provider -
I would have much preferred the following:
- A value of type T is bound to a context
which I would assume would look something like this:
```dart
Ref.bind<T>(
context,
T(),
)
```
somewhere else...
```
Ref.of<T>(context)
```
As opposed to the current implementation that is as follows:
```dart
final myTRef = Ref<T>();
```
somewhere else...
```dart
myTRef.bind(
context,
T(),
)
```
somewhere else once again...
```
myTRef.of(context)
```
The important distinction here is of course in how we access these Refs.
I have a strong preference for the type T being itself the key for access, over creating additional global variables that act as key.
I would argue as follows:
- Using T directly simplifies both creation and access considerably, as seen in the example above
- There are very little usecases where we need two or more distinct objects both of type T in the same context tree - even if, such situations can be easily circumvented by creating higher order objects that manage these smaller ones.
- The perceived benefit from making Reference variables private is minimal - access control is already enforced by location in the tree.
Admittably, I have not yet read the source code of the package, to find out whether instances of Refs to correctly find a value, is a necessity, or whether this is simply a design choice.
Please do let me know your thoughts on this matter.
关闭于 2025-02-10 4 条评论