Passing JSON to FutureBuilder returns null at first - Flutter

1,402 views
Skip to first unread message

Kristin

unread,
Jun 27, 2021, 12:11:43 AM6/27/21
to Flutter Development (flutter-dev)
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.

Suzuki Tomohiro

unread,
Jun 27, 2021, 12:18:38 AM6/27/21
to Kristin, Flutter Development (flutter-dev)
Call print only after you confirm “ if (snapshot.hasData)

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/101d4909-5ac8-4370-b8e6-b07b710cb4dfn%40googlegroups.com.

Kristin

unread,
Jun 27, 2021, 12:23:46 AM6/27/21
to Flutter Development (flutter-dev)
Well that's an oopsie on my part, now logs do not return null but returns the JSON. 
return Text(_getTweet(snapshot.data)); however prints null in the emulator, this is the _getTweet method:
String _getTweet(dynamic account) {
if(account == []){
return 'No Data';
}else{
return account[0]['text'].toString();
}
}
How can I get it to return "text" from my JSON? JSON looks something like this:  [{data: [{id: ..., text: ...}, {id: ..., text: ...}]} {meta: ...}]

wiracle

unread,
Jun 27, 2021, 7:12:17 AM6/27/21
to Kristin, Flutter Development (flutter-dev)
This is more a question of json decoding. You may search for flutter plugin for json serialization to support static types via code generation, but you can also begin with a dart:convert exposed object

wiracle

unread,
Jun 27, 2021, 7:16:02 AM6/27/21
to Kristin, Flutter Development (flutter-dev)

Suzuki Tomohiro

unread,
Jun 27, 2021, 8:57:40 AM6/27/21
to Kristin, Flutter Development (flutter-dev)
Use the debugger to figure that out.

Reply all
Reply to author
Forward
0 new messages