当前位置: 首页 > 知识库问答 >
问题:

未处理的异常:类型列表不是类型字符串的子类型无法获取json数据[重复]

詹斌蔚
2023-03-14

我不能从json获取数据使用flutter超文本传输协议请求,因为它得到了这个未处理的异常:类型列表不是类型字符串的子类型,并希望加载更多的数据时滚动到屏幕底部。有人能帮帮我吗...

postData。省道锉

class PostData extends StatefulWidget {
  const PostData({Key key}) : super(key: key);

  @override
  _PostDataState createState() => _PostDataState();
}

class _PostDataState extends State<PostData> {
  List<String> getData = List();
  ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    fetchFive();

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        fetchFive();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Load Page"),
          toolbarHeight: 65,
          backgroundColor: Colors.blueAccent[700],
        ),
        body: ListView.builder(
            controller: _scrollController,
            itemCount: getData.length,
            itemBuilder: (context, index) {
              return Card(
                child: GestureDetector(
                  child: ListTile(
                    title: Text(getData[index]),
                  ),
                ),
              );
            }));
  }

  fetch() async {
    final response =
        await http.get('https://my-json-server.typicode.com/typicode/demo/posts');

    if (response.statusCode == 200) {
      setState(() {
        getData.add(json.decode(response.body));
      });
    } else {
      throw Exception('Failed to load data');
    }
  }

  fetchFive() {
    for (int i = 0; i < 5; i++) {
      fetch();
    }
  }
}

获取数据。dart文件模型json

class Users {
  final int no;
  final String nosiri;
  final String lokasi;
  final String status;
  final String pemilik;
  final String thumbnailurl;

  Users(
      {this.no,
      this.nosiri,
      this.lokasi,
      this.status,
      this.pemilik,
      this.thumbnailurl});

  factory Users.fromJson(Map<String, dynamic> json) {
    return Users(
      no: json['no'],
      nosiri: json['no_siri'],
      lokasi: json['lokasi'],
      status: json['status'],
      pemilik: json['pemilik'],
      thumbnailurl: json['thumbnailurl'],
    );
  }
}

共有3个答案

许鸿志
2023-03-14

通过检查代码,您的响应是列表

List getData = List();

然后您需要将数据转换为如下所示的列表,然后在getData中逐个添加数据。然后您可以将数据添加到文本中。

ListTile(
  title: Text(getData[index]['title'].toString()),
),

您的fetch()方法如下

  fetch() async {
    final response = await http
        .get('https://my-json-server.typicode.com/typicode/demo/posts');

    if (response.statusCode == 200) {
      setState(() {
        final data = json.decode(response.body);
        for (var i in data) {
          getData.add(i);
        }
      });
    } else {
      throw Exception('Failed to load data');
    }
  }

柴衡
2023-03-14

作为响应,您将获得List

   [
      {
        "id": 1,
        "title": "Post 1"
      },
      {
        "id": 2,
        "title": "Post 2"
      },
      {
        "id": 3,
        "title": "Post 3"
      }
    ]
 

东方旭东
2023-03-14

尝试

List<Users> getData = [];

改变

getData.add(json.decode(response.body));

getData.addAll(List<Users>.from(json.decode(response.body).map((x) => Users.fromJson(x))));
 类似资料: