I created an async function to fetch the timeline of public tweets using Twitter API:
Future<List<dynamic>> fetchTweets() async {
List<dynamic> tweets = [];
var request = await http.get(Uri.parse(
if (request.statusCode == 200) {
tweets.add(json.decode(request.body));
return tweets;
} else {
return json.decode(request.reasonPhrase!);
}
}
Using FutureBuilder, I call the function and within try to display the data returned from the API however using print() to test the behavior I see in the logs that first the print() returns null and THEN after a second prints the data. This is the only print() in my code so I'm skeptical of this behavior:
child: FutureBuilder<List<dynamic>>(
future: fetchTweets(),
builder: (context, snapshot) {
print(snapshot.data);
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
if(snapshot.data != null){
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
padding: EdgeInsets.all(8),
itemCount: snapshot.data!.length,
itemBuilder: (BuildContext context, int index) {
return Text(_getTweet(snapshot.data));
//return Text(snapshot.data![0]);
});
} else{
return Text("Is null");
}
} else {
return Text('No data found');
}
} else {
return Center(child: CircularProgressIndicator());
}
This is what print() shows in my logs (otherwise there are no errors or no other indication that something is wrong):
> I/flutter ( 4718): null
> I/flutter ( 4718): [{data: [{id: ..., text: ...}, {id: ..., text: ...}
Would anyone have an idea of what I can do to get the data that prints the second time around to print from the first time? Currently all that is returned from the Text widget is null.