我正在try 从链接获取数据.以下是链接:- "https://wrestlingworld.co/wp-json/wp/v2/posts?categories=22"

在获取时,我收到错误消息.我试了各种方法,但找不到准确的解决办法.我附上了我的代码,我try 了什么,我做了什么,以解决问题.

这是我的控制器:

class NewsController{
  String url = "https://wrestlingworld.co/wp-json/wp/v2/posts?categories=22";
  Future<List> getNews() async {
    try{
      var response = await http.get(Uri.parse(url));
      if(response.statusCode == 200){
        print(response.body);
        // Map<String, dynamic> resp = json.decode(response.body);
        return json.decode(response.body);
        // return Map<String, dynamic> json.decode(response.body);
      }
      else{
        return Future.error("Error Handling the request");
      }
    } catch(Exception){
      print(Exception);
      return Future.error("Error in the controller");
    }
  }
}

这是我的模型:

class NewsModel {
  late int id;
  late String date;
  late String status;
  late String type;
  late String link;
  late String title;
  late String content;

  NewsModel(
      {
        required this.id,
        required this.date,
        required this.status,
        required this.type,
        required this.link,
        required this.title,
        required this.content});

  NewsModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    date = json['date'];
    status = json['status'];
    type = json['type'];
    link = json['link'];
    title = json['title'];
    content = json['content'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['date'] = this.date;
    data['status'] = this.status;
    data['type'] = this.type;
    data['link'] = this.link;
    if (this.title != null) {
      data['title'] = this.title;
    }
    if (this.content != null) {
      data['content'] = this.content;
    }
    return data;
  }
}



这里是我试图获取的地方:

FutureBuilder(
  future: newsController.getNews(),
  builder: (context, snapShot) {
    if (snapShot.hasData) {
      return Padding(
        padding: const EdgeInsets.all(8.0),
          child: ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: snapShot.data?.length,
              itemBuilder: (context, item){
                return NewsCard(title: snapShot.data?[item]['title']);
          })
      );
    }
    else {
      return const Center(child: CircularProgressIndicator());
    }
  },
),

try 提取时遇到此错误:类型‘_InternalLinkedHashMap&lt;字符串,动态&>’不是类型‘字符串’的子类型

有没有办法解决这个问题?

我试着在不使用Future的情况下编写函数,它根本不起作用.所以我想使用相同的函数来获取.

推荐答案

您在从MAP构造数据时出现了一些错误.This site对查看来自http请求的完整响应有很大帮助.

在您的情况下,以下数据类应该会有所帮助:

class YourModel {
  final int id;
  final String date;
  final String status;
  final String type;
  final String link;
  final String title;
  final String content;

  YourModel({
    required this.id,
    required this.date,
    required this.status,
    required this.type,
    required this.link,
    required this.title,
    required this.content,
  });

  factory YourModel.fromJson(Map<String, dynamic> json) {
    return YourModel(
      id: json['id'],
      date: json['date'],
      status: json['status'],
      type: json['type'],
      link: json['link'],
      title: json['title']['rendered'],
      content: json['content']['rendered'],
    );
  }
}

也可以在您的代码中try 以下内容:

FutureBuilder(
  future: newsController.getNews(),
  builder: (context, snapShot) {
    if (snapShot.hasData) {
      return Padding(
        padding: const EdgeInsets.all(8.0),
          child: ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: snapShot.data?.length,
              itemBuilder: (context, item){
                return NewsCard(title: snapShot.data?[item]['title']['rendered']);
          })
      );
    }
    else {
      return const Center(child: CircularProgressIndicator());
    }
  },
),

Flutter相关问答推荐

Flutter bloc -如何使用BlocBuilder?

所需引用Firebase处不存在任何对象

底部导航栏项目在抖动中不更改 colored颜色

允许冻结在初始化时使用工厂

如何在WidgetRef引用外部的函数中使用Riverpod刷新数据

在Android Studio的新用户界面中未找到热重新加载

audioQuery不会等待判断存储权限

将侧边菜单和底部导航栏放在同一Flutter 应用程序中

如何创建这样的按钮

模块是使用不兼容的 kotlin 版本编译的.其元数据的二进制版本是1.8.0,预期版本是1.6

无法从一个页面导航到下一个页面 (Flutter)

如何使用Riverpod提供程序正确构建搜索栏?

从所有设备注销,因为用户帐户已从 firebase flutter 中删除

如何(部分)更新 riverpod 中的 AsyncValue?

java.lang.IncompatibleClassChangeError:找到接口 com.google.android.gms.location.SettingsClient,

我想在一个屏幕上有两个相同区域的列表视图,我该如何设置?

Flutter 中出现错误空判断运算符用于空值

为什么容器会填满整个空间?

如何在屏幕按钮中排列 row() (我想在按钮中放置屏幕导航图标)

如何在 Flutter 中创建类似箭头标签的设计