0

I am trying to parse data from a Rest API inside a Dart/Flutter application. The JSON contains a field called budgets at the root, which contains a list of Words. I want to get a List from this JSON and showing table from the Json with the package json_table.

class _PresupuestoPageState extends State<PresupuestoPage> {

  List <Map<String, dynamic>> _data;

  @override
  void initState() {
    super.initState();
    fetchData();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Presupuesto"),
        centerTitle: false
      ),
      body: Container(
        alignment: Alignment.center,
        child: _data != null
            ? JsonTable(
                _data,
              )
            : Text("Loading..."),
      ),
    );
  }

Future<void> fetchData() async {
    try {
      final response =
          await http.get("http://pr_laravel.loc/api/budgets");
      if (response.statusCode == 200) {
        print(response.body);
        setState(() {
          _data = jsonDecode(response.body) as List;
        });
      } else {
        print("Some error: ${response.statusCode}");
      }
    } catch (e) {
      print(e);
    }
  }

}

2 Answers 2

1

Just change type of your _data from List <Map<String, dynamic>> to List <dynamic>.

Sign up to request clarification or add additional context in comments.

2 Comments

This send me this error: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast
that's because you're using the list items which are dynamic as Map try to cast each list item toMap()
0
List<YourModel> temp = _data.map((data) => YourModel.fromJson(data)).toList();

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

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.