CRUD Pattern with API in Flutter - flutter

I have a REST API running on the server to create the object. In case there are any validation errors, the API returns 422 with error messages in the response. Simple.
What I am trying to do
Show a form to the user to fill in. When the user submits the form, show the loading indicator till the response comes back from the server.
In case the server responds success, then show the details of the object created on the server (basically an ID is now assigned). On back button, the form should not be shown but the widget from where the Form was launched should be shown.
In case the server responds with an error, then show a message and give the user a way to go back to the form to resubmit the form with changes. Ideally, it would be great if the form could be displayed automatically with a snackbar showing the error (or like a web form - errors on top of the form)
Constraint - I would like to avoid having to recreate the Form widget and re-use the one in the stack in case of API error.
Current Implementation
I have implemented a widget with a Form widget in flutter. On submit, When the form is valid, I save the form into a local model object. And then post the JSON representation of that object to the server using a service object.
The service object returns a Future of that model object which I use to create the next widget (view model details) and push into the Navigator stack. This widget, uses a FutureBuilder with this future that it receives in the constructor.
In case the FutureBuilder receives an error, I display a message with the button, where I pop the stack from the Navigator and go back to the Form view on click.
If there is no error, then this view displays the object details. But the back arrow in the app bar goes back to the previous Form widget.
Here the problems start:
If I use pushReplacement on the Navigator when loading the view that has the Future, then I can't just pop to come back to the Form on error. If I don't, then I come back to the Form even on success.
At present the only way seems to be to handle the Future also in the widget that has the Form and go to the detail widget only if the Future succeeds.But this does not seem a clean way with the Form widget doing too many things.
Is there a better alternative or pattern to achieve this so that I can pop the Form view in case the Future is successful?

Related

Best architecture to propagate flutter state change from the bottom up?

I have a catalog (main page) that displays ads thumbnail, by taping on it, user is pushed a detail view from which it can tap on a message button which pushes the message screen. This message screen requires on some circonstances to be signed in: therefore it might display a button that pushes the sign in screen.
Overall here is the hierarchy:
catalogue -> ad detail view -> message screen -> sign in screen.
My use case is that when the user successfully signs in, I need to update the message screen.
I can propagate a 'onUpdate' callback down the tree but I am questioning that method.
Does Flutter offer a generic or better way to propagate state changes from the bottom up?
You can try
Using Global Variables for the State of the Button or
Using Provider
Personally for small Apps I use global Variables but as soon as it gets more complicated you should consider Provider.

How much should presenters know about the view?

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.

The route.first page (MyHomePage) is updating even without a callback, function or notification listener

I can't really share code for this as it's for my entire ~large~ application, but is it normal for a flutter application's lower navigation stack to be updated when the current page is?
For example, in this page I have a standard form with a few TextFormFields:
Whenever I click on one to start typing the page sets state as expected, but by adding print("Update"); inside the build function of the bottom page of the navigation stack, I can see that page is being updated too. It happens on all pages I put on top of the first route page. I've also been experiencing that the home page gets slower as the app has been open for longer, could this be a cause for that problem too?

update and pass data to next tab on navigation rail in flutter

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.

Asp.net MVC 3 partial view postback

I am new to mvc and I have a question
I have a page (asp.net Mvc 3) called profile which contains 3 partial view called (1) Address,Qualification,Experience.
Each of the partial view should allow you to add its details and save it details/contents without affecting the others.
How can I attach an action to each partial view save button and postback the result without refreshing other partial view.
Can someone explain this to me with and without using jquery/ajax
without ajax or jquery you'd be stuck with doing something horrible like iFrames... Stick to jquery, it is the best and easiest way.
Actually, here is one way you might be able to do it.
Name the "Submit" buttons according to the method you want called. Then, have all of your 3 sets of data inputs within one <form></form> which posts to an intermediate Controller endpoint.
In the intermediate controller endpoint, determine which button was pressed, then pass the correct data to your normal control methods, and push all the other data back to the view. That way you can fill out all the inputs, click the appropriate submit button, then only the correct data gets used! Then all the other data gets pushed back to the view and the approprate input boxes get filled out.