2021-06-27

Future method not passing JSON to Future Builder

I have an async method like so (printed the JSON from API to ensure the API returns value):

  Future<List<dynamic>> fetchTweets() async {
var headers = {
  'Authorization':
      'Bearer token',
  'Cookie':
      'guest_id=id; personalization_id=pers_id'
};
var request = http.Request(
    'GET',
    Uri.parse(
        uri));

request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
//prints the correct JSON
print(json.decode(await response.stream.bytesToString()));
  return json.decode(await response.stream.bytesToString());
} else {
  return json.decode(response.reasonPhrase);
}

}

When calling this method to the Future Builder, the snapshot contains no data:

            child: FutureBuilder<List<dynamic>>(
            future: fetchTweets(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                //snapshot has no data and therefore goes to the 'else' and prints 'No data found'
                print(snapshot.data);
                if (snapshot.hasData) {
                  return ListView.builder(
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      padding: EdgeInsets.all(8),
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext context, int index) {

                      });
                } else {
                  return Text('No data found');
                }
              } else {
                return Center(child: CircularProgressIndicator());
              }
            })

What am I missing here?



from Recent Questions - Stack Overflow https://ift.tt/2UIjG1u
https://ift.tt/eA8V8J

No comments:

Post a Comment