Dependency Injection for State objects

266 views
Skip to first unread message

Carlo Bernaschina

unread,
Jul 28, 2017, 4:13:01 PM7/28/17
to Flutter Dev
Dear All,
in my free time I'm trying to add Flutter support to a tool I'm building in the university (http://ifmledit.org | https://youtu.be/y_hDVeUbi7g).

I'm having troubles with the dependency injection I need to do between State objects.
To avoid to have a huge central State class that is managing everything I would like to decentralize the logic and put it in the State of the sub-components that build the UI.

In the examples of the Flutter framework that are managing interaction with external entities (HTTP, Files) the non Flutter specific objects are generally created directly from the code in the State object.

I would like to have an object which exposes services and events and to inject it into child widget States from the root widget.

The doubt I have is: how should I inject this object?

If I pass the object as a parameter to the widget, every time a build function is invoked and the widget is changed the State should unsubscribe from the events and subscribe again or at least check that the injected object is not changed every time the correlated widget changes.

I think that from a general point of view the question is: how can I send some initialization values to the State constructor / initialize?

---
Carlo BERNASCHINA

Google Intern

Andrew Wilson

unread,
Jul 28, 2017, 4:21:50 PM7/28/17
to Carlo Bernaschina, Flutter Dev
One thing we do in Fuchsia sysui is use an in-house Model dart class when acts as an inherited Widget to provide state to any Widget that needs it that's a descendant of where the state actually exists in the widget tree.


and then have a look at how we use it Armadillo here: https://fuchsia.googlesource.com/sysui/+/master

Otherwise, you can roll your own thing which uses flutter's InheritedWidget to pass data down the Widget tree.  I recommend that as the best way to "inject" things into a Widget's State.

-Andrew



--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Wilson

unread,
Jul 28, 2017, 4:25:24 PM7/28/17
to Carlo Bernaschina, Flutter Dev
Example usage:

main() {
MyModel myModelWithStuffIWantToInject = new MyModel();

runApp(new ScopedModel<MyModel>(model: myModelWithStuffIWantToInject, child: new MyApp()));
}

MyApp extends StatefulWidget {...}
MyAppState extends State<MyApp>{
...
Widget build(BuildContext context) => new ScopedModelDescendant<MyModel>(builder:(BuildContext context, Widget child, MyModel myModel) => new MyWidget(foo: myModel.foo, child:child), child: new BarWithDoesn'tDependOnMyWidget());
}

((Or something like that))

Andrew Wilson

unread,
Jul 28, 2017, 4:29:04 PM7/28/17
to Carlo Bernaschina, Flutter Dev
Let me know if you have any questions on how to make it work for you if you have any trouble.  We find it extremely useful.  I was considering this morning whether Flutter may want to incorporate it directly into their API set.
Reply all
Reply to author
Forward
0 new messages