0

I am trying to get my data to show on my flutter app from a api (formatted in json) created by connecting mysql on node.js with express.

i am running this error in android studio console

[VERBOSE-2:dart_error.cc(16)] Unhandled exception:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 
'List<dynamic>'

the api currently looks like this

{
    "error": false,
        "data": [
           {
              "name": "conner",
              "age": 24
            },
            {
              "name": "andrew",
              "age": 20
            }
         ],
         "message": "Todos list."
         }

I am using this tutorial and just replaced his value "title" with mine "name", https://www.youtube.com/watch?v=-PRrdG163to

here is the code I'm running

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(new MaterialApp(
    home: new HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => new HomePageState();
}
class HomePageState extends State<HomePage> {

  Map<String,dynamic> data;

  Future<String> getData() async {
    var response = await http.get(
        Uri.encodeFull("http://10.1.0.109:8080/names"),
        headers: {
          "Accept": "application/json"
        }
    );

    this.setState(() {
      data = JSON.decode(response.body);
    });
    print (data[1]["data"]);

    return "Success!";
  }

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Listviews"),
      ),
      body: new ListView.builder(
        itemCount: data == null ? 0 : data["data"].length,
        itemBuilder: (BuildContext context, int index) {
          return new Card(
            child: new Text(data[index]["data"]),
          );
        },
      ),
    );
  }
}

1 Answer 1

1

the problem is in the structure of your JSON file it's not the same as in the tutorial so try this

data["data"][1]["name"]

NOTE :

change data from list to var or Map<String,dynamic>

Map<String,dynamic> data;

after that you need to get the list length so in the itemCount change it to

itemCount: data == null ? 0 : data["data"].length,

Full Example

enter image description here

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

13 Comments

but name needs to be a string doesn't it, ill get compiling errors otherwise
sorry i did i small typo in the code try the edited one
see the edited answer i tried it and it works the problem was on the type of the data variable i didn't notice that its not a list
sorry again i updated my answer the second problem was on the list length
even with this? if so the problem would be in the API it self. did you try to test it on the browser ?
|

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.