This source is based on this repository and update to understand GetIt.
The original repo author is also introduced own idea in this Qiita article. (Only Japanese)
I'm learning and developing Flutter app. Recently I was struggling with DI in my flutter app. In internet, there are many info for DI on flutter and the majority seems provider packege and InheritedWidget
. However, I was having some strange feeling for them because they seemed tightly integrated Widget tree. And I don't want to think the context of the widget tree when obtaining an instance which is widely needed from any widget or business logic.
One day, I found get_it packege ad another candidate for DI in my flutter app.
Thus, I tried to understand to update from an example app which is using InheritedWidget
to new way using get_it
package.
This is 1st screen of this app. User can see random words in the list and can mark as "Favorite".
User can see total number of favorites on the navigation bar and it will be updated by adding or removing favorites.
From an icon button on the navigation bar, user can move 2nd page: Your Favorite page.
User can review own favorite words in the list. As default, the list include a dummy suggestion item at the first place of the list. Also, dummy ad Views will be shown as items of the list.
User can disable to show the dummy suggestion item and dummy ad views by using a switch on the navigation bar.
Also, user can remove favorites from the delete icon button for each favorite item, if user doesn't like it any more.
- File structure upadte.
- Add some debug log.
- Use get_it package
- Add initialization step in
main.dart
. - Remove logic using
WordBlocProvider
class which was a subclass ofInheritedWidget
. - Use
GetIt.I<WordBloc>()
instead of the Provider class. - Refactor some methods to not use
WordBloc
as an argument because any logic can getWordBloc
instance from GetIt.
- Add initialization step in
- In Your Favorite Page, added delete icon button for each
ListTile
and call remove function ofWordBloc
when tapped. - Add new getter
itemsWithInfo
inWordBloc
to provideStream
for prividing a list including all of favorited words, one suggestion and ads. This stream is created bytransform
ing streams, from the original stream:Stream<List<WordItem>>
.