I'm trying to make a simple App to stretch my legs and implement MVVM using Flutter which I have never done.
On the main page of the App, I'm displaying items from a collection fetched from the backend. So my logic would be that when the page appears, the MainPageViewModel is initialized and the area that should display the collection should show a loader. During the initialization, the call to the back end is made. Once this call resolves, I would store the result in a variable and somehow notify the View that it can update its visuals, either to display an error or the collection that's been fetched. It's especially that notifying the view that it should update that stomps me. I'm also guessing it'd be good practice to not render the view until the viewModel has finished initializing.
I'm curious how you reckon this should be made. I know I could just dump it into a FutureBuilder but my goal here is to learn the MVVM concepts to start doing more complex stuff.
Thanks in advance!
Related
I have a Flutter mobile application where I do request data from the backend, and each individual record of this data have it's own identifier and category. For each of these identifiers I want to display them properly and also redirect the user to the correct page of the app. An example:
User enter the page, and we request the data
Data arrives and we present it.
When clicking at each item, we should redirect the user to the appropriate page of that item.
Question 1) Where should the route definition be?
Currently I created a Widget and made a switch case on the identifiers to define the route of each one click.
But shouldn't this be done already in the presenter, so the view would only display it and create the callbacks to redirect it with the given route? My concern about it is that then the presenter would know too much about the View, or am I wrong?
Question 2) Each of those items that I mentioned have their priority, and in the view each priority is displayed in their own way, like Urgent priority is displayed in red and so on...
Should presenter also define these colors for the view? The problem is that this Color it's an object from the Flutter Framework which I would like to avoid in this layer...
My current implementation is like this:
View calls controller (bloc) which call usecase that responds with the needed data.
Presenter (same bloc) gets this data and format the correct title for each identifier
The view gets the view model from presenter and displays it, but also need to do a switch case on the identifier of each item to know about the color of the item on the screen and also the route to be redirected when clicked.
Any help is appreciated.
I am using navigation rail and I am trying to pass data from one tab to next from the body but I am unable to do so and I haven't find any resource on it. Kindly guide me ,if there is any other widget that can perform the task I am trying to achieve
For Passing Data Between Widgets, you need to pass it from a common parent. in your case, your first page should save its data in the MainPage(The Page holding the NavigationRail) and then the second Page gets its data from MainPage.
This type of problem is more commonly solved by using a state management solution. The Flutter site has a list of them here.
Provider is an easy to use package that can provide an object throughout your widget tree. this way you can get a shared object between the two pages and use it to exchange data.
I have a page from which I navigate to another page and do something there , when I press back button, the current page simply pop and previous page keep as it is. I have to refresh it to make changes.
I want, when I make change to second page, first page should automatically get updated according to it.
You should read a little about managing state in flutter.
There are few very good ideas on how to approach this problem.
You can try with fairly simple Scoped Model.
or little bit more confusing Inherited widgets. If you ever used Theme.of(context) or Navigator.of(context) that is basically inherited widget. Just a little more harder to set up properly.
Check this amazing presentation on managing it from google io 2018
https://youtu.be/RS36gBEp8OI
I am trying to navigate on a frame with pages but instead of giving views to the page I am giving view models.
This works fine, but the problem is when I navigate between the pages the status of the radio buttons for example doesn't stay the same. It stays only if I use the views and not the view models.
Any ideas?
Thanks.
Some code to explain how you navigate between each control would allow us to give you more specific answers, currently we can only guess what you are doing.
It sounds like you are creating a new instance of each ViewModel when you are navigating between pages. This of course means things such as radio buttons and control states will not remain consistent.
You could use a framework such as MVVM Light and make use of their ViewModelLocator pattern. This means you could have one static instance of each ViewModel.
You could also store all of these states in a simple data model, and then simply have your new ViewModel instance refer to this model and update it's check boxes etc appropriately.
I'm trying to use the GWT Activity & Place model, but I'm having some
troubles with it about how to use my activities.
I've got a LoginActivity which drives the user to another activity :
DemandsActivity.
My DemandsActivity manages a view ("DemandsView") which displays a
simple list of demands (with a CellTable).
The whole works fine.
I would like to be able to show the details of a demand, from a
selected line of my cellTable, by displaying
a DialogBox with the informations.
I thought I could use one more
activity to do that : DemandDetailsActivity.
But I don't know how to do that.
Or I've been wrong from the beginning. Maybe should I put several presenters (displays) into my activity ? One presenter to display my CellTable, and another one to display a selected element of my CellTable in a DialogBox, without changing Place ?
What do you think of that ?
Thanks
What you are trying to do is called master-detail view. People have been implementing it with GWT, just google around.
On a side note: in MVP parlance Activities are presenters and Views are displays, so when you say put several presenters (displays) into my activity it really makes no sense.
Presenters should correspond to a place and handle business logic. They should not be concerned with the display part. And they should be testable, which means they should run on desktop/server JRE without GWT client dependencies.
So, all the GUI building part should be inside Views. And, yes, you could have multiple Views per Activity if this makes sense. BUt, personally, I'd go with one View that shows details (possibly dialog) when Activity instructs it to.
You should normally have a one to one relationship between Places and Activities but you may have many Views per a given Activity. In the project I'm currently working on we create an interface per Presenter and its associated View and then have our Activities implement any Presenters for the Views it needs to display.