flutter getx, getview with two controllers - flutter

I'm a little bit confused with how GetView works. I currently have a page that needs to have at least 2 different controllers. From what I've seen, people mostly use GetView as opposed to Get.find() for better code format, however GetView seems to only use 1 controller, for example:
class HomeScreen extends GetView<HomeController>
I have tried to google it and haven't found any solution. I also have tried to use
class HomeScreen extends GetView<HomeController> with GetView<UserController>
But it gave me an error that says
The class 'GetView' can't be used as a mixin because it extends a class other than 'Object'.
I would very appreciate if you can give me a solution or a workaround to use GetView with two or more controllers.
Please note that I'm using GetX bindings (therefore I have already wrote Get.put(Controller()) in another file) and I want to avoid using Get.find in my widget tree.

Can you please explain what's the usecase of using GetView with multiple controllers? It's not supported. Because a view can have multiple controllers but how many?
Therefore, in that case, you should just use the GetX widget for different controllers like:
GetX<Controller1>(),
GetX<Controller2>(),
and so on

Related

Where is Flutter's Navigator declared?

When creating a Flutter application,
we use a Navigator to move back and forth between screens.
In my application, I have some class instances that
I want to share with all child widgets in the hierarchy.
An example is a Data Base driver class (stateful)
which holds all functionality of reading/writing to the actual database.
Currently - the 'main' function is initializing it,
then the resulting object is passed through all
constructors of the participating widgets.
How do I make it behave like the Navigator
so it would be available in the global scope
without importing it or passing it through a chain of constructors?
I would gladly accept any other approaches,
and still, if you can also address the exact question - I would be grateful :)
I opened the code of Navigator, Stateless and Stateful widgets,
I tried to google for the source of that Navigator instance
but came up with nothing helpful.
Have you tried using InheritedWidget?
From the docs:
Base class for widgets that efficiently propagate information down the tree.
Create DataBaseDriver class as inherited Widget class, access it in its child class using context as DataBaseDriver.of(context)

why do we need to extend statefull or stateless classes rather create an object of them and use it in flutter?

I am new to flutter, I wonder why we extend Statefull or stateless classes to create our own widgets, why not creating the objects of the instead. Now many others might say it has build function that needs to be overridden but I guess it can also be done inside the object. Please give me an explanation.
flutter has lifecycle, widget will appear after build
https://i.stack.imgur.com/94idE.png
Both Stateful and Stateless classes are abstracts.
So that, to use methods such as initState in Stateful or "build" in both kind of widgets you need extend either Stateful or Stateless (exactly "extends" keyword, if you use "implements" keyword, you will be forced to implement all of the methods described in abstract).
More about:
StatefulWidget
StatelessWidget

What is the best form to pass data between screen with GetX?

I am new on Flutter, and I am using GetX.
I want to do a Stepper for registering, and I want to pass one object between 4 screens and fill some data on each screen.
What is the best method to do that? I was thinking to put a UserModel on a controller and pass it between screens.
Thank you so much.
Generally you don't need to manually pass around data to different pages when using most state management solutions. You store the relevant data in a single instance of the object that you access from anywhere.
class StepController extends GetxController {
// any data in this class is accessible from anywhere and doesn't
// need to be manually passed to any pages
}
Use the variables you create in the GetX class for the Steps and then from anywhere in the app you find the controller with
final controller = Get.find<StepController>();
Just make sure you initialize once it at some point with
Get.put(StepController());
I haven't use GetX but MobX(+provider). In your case, I think dependency injection is best way to share store between screens, you can consume the stores everywhere, so you dont need to pass data to other screen.

Flutter : ValueNotifier+ValueListenableBuilderとStateについて

https://flutter.dev/docs/cookbook/effects/photo-filter-carousel#interactive-example
I have a question about the sample at the end of the above page.
However, the question itself has little to do with the subject matter on the above page.
ExampleInstagramFilterSelection is a subclass of StatefulWidget.
State management seems to be done with ValueNotifier + ValueListenableBuilder.
Looking at the code, I thought that ExampleInstagramFilterSelection could be a Stateless Widget, so I tried making it a Stateless Widget and tried it.
There is no particular error, and it looks exactly the same as before the change.
I can't think of the reason why ExampleInstagramFilterSelection is (a subclass of) StatefulWidget like the sample (why it's better to make it a subclass of StatefulWidget). Is there anything?
I can only think of "We might need State in the future", but then I feel like we should just make it a StatefulWidget when we need it.
You are correct, the ExampleInstagramFilterSelection in the above example could be simplified by converting it into a StatelessWidget.
As it is, there is no need to use a StatefulWidget, because the Widget does not need to redraw itself because of a changed member.
All the state handling is done in FilterSelector.

Why does a stateful widget build itself using a build method in the state class

I'm just learning flutter - I build a few basic tutorials and am now trying to build a simple notepad application.
https://github.com/sketchbuch/examples_flutter/blob/master/notes/lib/src/components/notepad/notepad.dart
In this file I have a stateful widget which has a state property. What I don't understand is why I need the stateful widget at all... the state class seems to be doing everything including building the list of notes.
Am I doing this right? It doesn't seem right that state builds the whole stateful widget...maybe it is just because my app is basic at the moment but the stateful widget doesn't seem to do anything at the moment.
I just would have thought that the staefulwidget would handle rendering and state would just be an object storing properties
As pointless as an empty StatefulWidget subclass looks like, it is necessary. That class is what allows Flutter to manipulate the State subclass.
Without a StatefulWidget, Flutter is unable to create/update/dispose of a State.
StatefulWidget subclass is also a way to customize the behavior of State by adding fields in StatefulWidget and using them in the State class.
You're saying
My Notepad widget as such is not doing anything but my _NotepadState is. Then why should I use the redundant Notepad widget?
Why is it required?
Your Notepad widget is not alone. It has super classes. It extends StatefulWidget which in turn extends Widget (which extends DiagnosticableTree which extends Diagnosticable).
The super classes are doing all the heavy lifting for you and that you had to write almost zero code. Mostly it is used to accept parameters and create state.
Hope that helps!