Dependency Injection without external packages #249
-
Hi @rodydavis, I know signals is a reactivity library but I was thinking...
Would it be a good practice achieve DI in signals with untracked? Like an AuthenticationRepository class signal? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
It has what @rodydavis calls an "experimental" inherited widget in V5 See https://dartsignals.dev/guides/dependency-injection/#inheritedwidget and https://pub.dev/documentation/signals/latest/signals_flutter_extended/SignalProvider-class.html and also a It is a start at least, I think Rody might have some plans on how to evolve this further in a later release. I recommend checking out |
Beta Was this translation helpful? Give feedback.
-
For never subscribing and never mutating you could create a signal like this: final ReadonlySignal<Database> db = (){
final Database connection = ...;
final instance = signal(connection, autoDispose: true);
instance.onDispose(() => connection.close());
return instance;
}(); Or simpler approach that just makes a value readonly and a singleton: final ReadonlySignal<SharedPreferences> prefs = signal(...);
void main() async {
// Explicitly cast to set value
(prefs as Signal<SharedPreferences>).value = await SharedPreferences.getInstance();
...
} For both of these, For MultiProvider you can actually use |
Beta Was this translation helpful? Give feedback.
For never subscribing and never mutating you could create a signal like this:
Or simpler approach that just makes a value readonly and a singleton:
For both of these,
.value
will cause a 1 time subscription, but what you want is.peek()
which will just return the value without subs…