Retrieve data without using FutureBuilder
The problem you're having is caused by the fact that you probably don't have an architecture setup for your app so your state, business logic and ui code is being mixed all into one place.
What you want to do is be able to request data independently of having it tied to a FutureBuilder (I recently did the same thing). You need to firstly separate all your operations logic from your UI so you need some kind of architecture for that. There are lots of them but the two I have found most useful is:
Scoped Model. For a decent scoped model tutorial look at this
Redux (Overkill in your current situation)
As an example, this is a function in my notices_model file.
Future fetchNotices() async {
if (_notices == null || _notices.length == 0) {
_notices = await _mobileApi.getNotices();
notifyListeners();
}
}
the _notices you see there is a local variable of type List that I expose through a property. So in short.
Setup an architecture that splits your view logic from your operations / business logic
Bind to the properties in your view logic and just perform your operations normally.
Also take a look at the FlutterSamples for architecture and examples on their github
Pierre Le Brun
Updated on December 08, 2022Comments
-
Pierre Le Brun less than a minute
I want to retrieve my data without using the method FutureBuilder
This is my method :
Future<bool> fetchJointures(http.Client client) async { final response = ('{"isInteresses": false}'); return compute(parseJointures, response.body); } bool parseJointures(String responseBody) { final jsonParsed = json.decode(responseBody); return jsonParsed['isInteresses']; }
and how this example :https://flutter.io/docs/cookbook/networking/background-parsing do to display the data :
FutureBuilder<bool>( future: fetchJointures(http.Client()), builder: (context, snapshot) { if (snapshot.hasError) print(snapshot.error); return A_Widget(data : snapshot.data); }, );
i want to retrieve and store my data in a var like this :
bool data = snapshot.data;
Finally i search how i can retrieve my data and store it in a var and not in param of a widget.
-
diegoveloper almost 4 yearsSo what do you want to use instead of FutureBuilder?
-
Pierre Le Brun almost 4 yearsI want to store my data in a var. Not store it as param in a widget
-
diegoveloper almost 4 yearswhy don't you want to use FutureBuilder? it's necessary because your method is async and returns a Future
-
Pierre Le Brun almost 4 yearsI need to use a bool as a parameter and i cant use FutureBuilder because when i try to call it, it says "Boolean parameter is not a subtype of FutureBuilder<bool>"
-
diegoveloper almost 4 yearsyou are doing something wrong, inside your FutureBuilder, your snapshot.data should be a bool
-
-
Pierre Le Brun almost 4 yearsYeah my architecture setup is actually null xD I will take a look to Redux(I use it with React), thanks
-
Filled Stacks almost 4 years@PierreLeBrun if you're making a small app I would recommend ScopedModel. It's also controlled with one state but you can split your models up for different features. Just put your Future function in the model and call it, no need to await, just handle everything inside properly and save your results in a local model
-
Pierre Le Brun almost 4 yearsOk i will do this, i don't have many Future functions (4) and it's good to know how to work more efficiently.
-
Pierre Le Brun almost 4 yearsThanks, i will test that