Retrieve data without using FutureBuilder

2,178

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:

  1. Scoped Model. For a decent scoped model tutorial look at this

  2. 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.

  1. Setup an architecture that splits your view logic from your operations / business logic

  2. 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

Share:
2,178
Author by

Pierre Le Brun

Updated on December 08, 2022

Comments

  • 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
      diegoveloper almost 4 years
      So what do you want to use instead of FutureBuilder?
    • Pierre Le Brun almost 4 years
      I want to store my data in a var. Not store it as param in a widget
    • diegoveloper
      diegoveloper almost 4 years
      why don't you want to use FutureBuilder? it's necessary because your method is async and returns a Future
    • Pierre Le Brun almost 4 years
      I 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
      diegoveloper almost 4 years
      you are doing something wrong, inside your FutureBuilder, your snapshot.data should be a bool
  • Pierre Le Brun almost 4 years
    Yeah my architecture setup is actually null xD I will take a look to Redux(I use it with React), thanks
  • Filled Stacks
    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 years
    Ok 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 years
    Thanks, i will test that