6

this is my first flutter JSON example, I tried to fetch data from the json link and display the titles as list view

I used this code to write mine with some changes .. in the link he has results array in mine I haven't so I left the data = resBody[" "]; empty is that right?

also, I got this error with the flutter HTTP request and don't really what to do

any help, please?

Dart Error: Unhandled exception:
 type 'String' is not a subtype of type 'int' of 'index'

this is my code

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

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

class ToDo extends StatefulWidget {
  @override
  ToDoState createState() => ToDoState();
}

class ToDoState extends State<ToDo> {
  final String url = "https://jsonplaceholder.typicode.com/todos";
  List data;

  Future<String> getTitle() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});

    setState(() {
      var resBody = json.decode(res.body);
      data = resBody[" "];
    });

    return "Success!";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Title List"),
        backgroundColor: Colors.amber,
      ),
      body: ListView.builder(
        itemCount: data == null ? 0 : data.length,
        itemBuilder: (BuildContext context, int index) {
          return new Container(
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Card(
                    child: Container(
                        padding: EdgeInsets.all(15.0),
                        child: Row(
                          children: <Widget>[
                            Text("Title: "),
                            Text(data[index]["title"],
                                style: TextStyle(
                                    fontSize: 18.0, color: Colors.black87)),
                          ],
                        )),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    this.getTitle();
  }
}
1
  • 1
    What line causes this error? Commented Jan 14, 2019 at 10:05

2 Answers 2

3

Your getTitle methods needs to be changed to the following:

Future<String> getTitle() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});

    setState(() {
      var resBody = json.decode(res.body);
      data = resBody; //This line changed from data = resBody[" "];
    });

    return "Success!";
}
Sign up to request clarification or add additional context in comments.

Comments

-1

I got this error when I forgot to add accept type in request header

Comments

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.