Shared controller is multiple views
SimonVillage opened this issue · 1 comments
I am trying to understand how I could share controllers with different views.
We have two views which use the same widgets with the same content. This content can come from the same controller as it displays the same data.
Let's assume the following structure
- /modules
- /news
- page.dart
- controller.dart
- binding.dart
- repository.dart
- /prices
- page.dart
- controller.dart
- binding.dart
- repository.dart
now the news view is displaying a widget, which is also used in prices, which is defined as a global widget. My assumption is that we should avoid accessing the prices controller from the news view?
As a controller should be responsible for a view, I would assume that the global widget should not have it's own controller?
Hello, in this case I recommend the use of a service, as it will be available throughout the application and can be programmatically transmitted or reinserted.
If you want to maintain a Controller approach, you have to observe one condition:
Your controller is not / cannot be removed from memory, for example, not going to other pages with Get.off.
This way you can use, directly on your page, or even recovering in another driver's onInit as follows
In controller:
onInit ...{
final anothercontroller = Get.find<AnotherController>();
}
or
In page:
Text(Get.find<AnotherController>().user.name)