mvvm - How to Call ViewModelProvider without ref in Flutter - Stack Overflow
What I Want to Achieve:
I want the search results to be updated every time a character is inputted (or changed) in the search box. I aim to implement this feature as close to clean architecture as possible.
I Use: flutter / flutter-riverpod / get_it
Assumptions:
- Bind the input value of the search box to the
query
inSearchViewModel
. - Call
ExploreController
every time a change notification is received. - have
ExploreController
callSearchInteractor
. - After obtaining the search results within
SearchInteractor
, haveSearchInteractor
call and pass the value toSearchPresenter
. SearchPresenter
then callssetItems
ofSearchViewModel
.
It's peculiar that a ViewModel is calling another ViewModel within itself. However, I thought this flow was okey when using data binding and trying to implement clean architecture. If my assumptions are incorrect, please point it out.
Sample Code
I want to write the following code but,
Since I am using provider
, I need to pass ref
from the controller to the presenter to call the viewmodel. This causes unrelated layers to depend on flutter_riverpod
and leads to an increase in unnecessary passing of ref
. This is causing issues for me.
final searchViewModelProvider =
ChangeNotifierProvider((ref) => DI<SearchViewModel>());
class SearchViewModel extends ChangeNotifier {
final ExploreController _exploreController;
SearchViewModel(this._exploreController);
List<MyResult> _filteredItems = [];
String _query = "";
String get query => _query;
set query(String value) {
_query = value;
_fireController();
notifyListeners();
}
List<MyResult> get filteredItems => _filteredItems;
set filteredItems(List<MyResult> values) {
_filteredItems = values;
notifyListeners();
}
Future<void> _fireController() async {
if (_query.isNotEmpty) {
_exploreController.search(_query);
}
}
}
//controller
//flutter/material.dart have searchcontroller class
class ExploreController {
SearchInteractor _searchInteractor;
ExploreController(this._searchInteractor);
void search(String query) {
_searchInteractor.execute(query);
}
}
//usecase
class SearchInteractor {
MyRepository _myRepository ;
SearchPresenter _presenter;
SearchInteractor(this._myRepository,this._presenter);
void execute(String input) async{
List<MyResult> results=await _myRepository.getMyResults(input);
_presenter.execute(results);
}
}
//presenter
class SearchPresenter {
SearchViewModel _viewModel ;
SearchPresenter(this._viewModel);
void execute(List<MyResult> input) async{
_viewModel.filteredItems=input;
}
}
final DI = GetIt.instance;
void setupLocator() {
//repository
DI.registerSingleton<MyRepository>(() => MyRepository());
//viewmodel
DI.registerFactory<SearchViewModel>(
() => SearchViewModel(DI<ExploreController>()));
//controller
DI.registerFactory<ExploreController>(
() => ExploreController(DI<SearchInteractor>()));
//usecase
DI.registerFactory<SearchInteractor>(() => SearchInteractor(
DI<MyRepository>(), DI<SearchPresenter>()));
//presenter
DI.registerFactory<SearchPresenter>(() => SearchPresenter());
}
- VMware宣布与Yahoo收购电子邮件和协作软件提供商Zimbra的协议
- i need help troubleshooting issues with plugins, i am using gradle 8.12 - Stack Overflow
- powerbi - I have two table Fact table and Dimention table and I am trying to calculate Past year sales .Value its not working -
- pytorch - Why can't my DDPG solve MountainCarContinuous-v0 - Stack Overflow
- reactjs - Next.js - I need static error page template in non-static app router based app - Stack Overflow
- macos - Can't access UI "section" in AppleScript - Stack Overflow
- express - How can I access a cookie (refreshToken) on the server side in Next.js? - Stack Overflow
- python - Custom font in a style sheet in matplotlib - Stack Overflow
- python - Why is my KivyMD Button not changing Color with "md_bg_color"? - Stack Overflow
- youtube - How to disable the Page Visibility API of react native webview? - Stack Overflow
- logging - C++ spdlog: How to append output to last log message? - Stack Overflow
- How to make smooth animations in Android Compose for expanding TextField? - Stack Overflow
- smartcontracts - FailedCall() with OpenZeppelin meta transactions (ERC2771Forwarder and ERC2771Context) - Stack Overflow
- javascript - Unable to locate tests within a folder - Stack Overflow
- python - NaN loss output when training DNN with Keras - Stack Overflow
- micropython - Bidirectional communication over USB between host PC and Raspberry Pi Pico - Stack Overflow
- Swift Predicate: Fatal Error Keypaths With Multiple Components - Stack Overflow