1

enter image description here

I want to parse my JSON data and display all the nodeValues of the rows and not just [7] (which contains the word hello), however my FutureBuilder doesn't display the JSON data (stuck on CircularProgressIndicator) even though i'm following the correct JSON path.

//Updated code
class Feed {
  String title;

  Feed({this.title});

  factory Feed.fromJson(Map<String, dynamic> json) {
    return Feed(
        title: json["data"]["tables"][0]["rows"][7]["cols"][1]["nodeValue"]);  
  }
}
//I am making a post method to an API that returns me a JSON output.
Future<List<Feed>> post() async {
  final Response<String> result =
      await Dio().get('https://example.com');
  String _baseUrl = "https://html2json.com/api/v1";
  var options = Options(
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    followRedirects: false,
  );

  final response = json.decode(result.data);

  final responseJson = await Dio().post(
    _baseUrl,
    data: response,
    options: options,
  );

  if (responseJson.statusCode == 200) {
    return (response as List).map((json) => Feed.fromJson(json)).toList();
  } else {
    return null;
  }
}
  //This is stuck on CircularProgressIndicator();  
FutureBuilder(
              future: post(),
              builder: (context, AsyncSnapshot<List<Feed>> snap) {
                if (snap.hasData) {
                  return ListView.builder(
                      itemCount: snap.data.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Text(snap.data[index].title);
                      });
                } else {
                  return CircularProgressIndicator();
                }
              });
4
  • Are you parsing it with the json constant available in Dart? api.flutter.dev/flutter/dart-convert/json-constant.html Commented Oct 13, 2020 at 11:45
  • Seems like your post() returning list of Feed objects. So, try using Feed member snap.data[index].someVariable Commented Oct 13, 2020 at 11:46
  • Could you check my updated code once please? @JoãoSoares Commented Oct 13, 2020 at 12:30
  • Yes, can you check my code once @Blasanka Commented Oct 13, 2020 at 12:31

1 Answer 1

1

I changed a few things to make your code work with the json place holder. You were using response.statusCode == 200, but response has no status code, the status code is on the var link.

class Feed {
  String title;

  Feed({this.title});

  factory Feed.fromJson(Map<String, dynamic> json) {
    return Feed(title: json["title"]);
  }
}

Future<List> post() async {
  final Response<String> result = await Dio().get('https://jsonplaceholder.typicode.com/todos');
  final response = json.decode(result.data);
  if (result.statusCode == 200) {
    return (response as List)
        .map((json) => Feed.fromJson(json))
        .toList();
  } else {
    return null;
  }
}

return FutureBuilder(
       future: post(),
       builder: (context, AsyncSnapshot<List> snap) {
              if (snap.hasData) {
                      return ListView.builder(
                          itemCount: snap.data.length,
                          itemBuilder: (BuildContext context, int index) {
                            return Text(snap.data[index].title);
                          });
                    } else {
                      return CircularProgressIndicator();
                    }
                  });
Sign up to request clarification or add additional context in comments.

6 Comments

It didn't work, I updated my code, could you please check it out? @Luiz ?
Can you debug the function? Is execution running up to Feed.fromJson?
I am getting my response from the api, but the snapshot doesn't contain any data
Your json has a "data" key ? in fromJson json["data"].
Yes the response is within data and then tables
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.