Pierre Le Brun
Pierre Le Brun

Reputation: 522

Retrieve data without using FutureBuilder

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.

Upvotes: 2

Views: 3518

Answers (2)

Bostrot
Bostrot

Reputation: 6033

You can store data normally even when you are using a FutureBuilder. You also do not need to specify what var type you want to return. Try this:

var data;
var initialFutureData;

new FutureBuilder(
    future: fetchJointures(http.Client()), // a Future<String> or null
    initialData: initialFutureData,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
    switch (snapshot.connectionState) {
        case ConnectionState.none:
        return Center(child: new Text('No connection...')); // error output
        case ConnectionState.waiting:
        return Center(
            child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: new CircularProgressIndicator(), // waiting indicator
        ));
        default:
        if (snapshot.hasError) return Center(child: new Text('Error: ${snapshot.error}'));

        initialFutureData = snapshot.data; // store data in var for fast reloading
        data = snapshot.data; // store data

        return A_Widget(data: snapshot.data); // return final widget when successfull
        }
    }),
);

Upvotes: 0

Filled Stacks
Filled Stacks

Reputation: 4356

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

Upvotes: 1

Related Questions