值类型 'post' 不能从方法 getPosts return 编辑,因为它有一个 return 类型的 Future<List<String>?>

A value type 'post' can't be returned from the method getPosts because it has a return type of Future<List<String>?>

嗨,伙计,我有一个错误值类型 'post' 不能从方法 getPosts returned 因为它有一个 return 类型的 Future当试图在 flutter 中连接 api 时,有人知道我该如何解决这个问题吗?当我使用其他一些虚拟 json 数据时它工作正常,但是当我切换到新闻时 api 我开始收到错误消息。

这是服务页面

import 'package:new_cdsc/Model/post.dart';
import 'package:http/http.dart' as http;

class RemoteService {
  Future<List<String>?> getPosts() async {
    var client = http.Client();
    var uri = Uri.parse(
        'https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=a04fc11949fc4633aa00fb01f37957e7');
    var response = await client.get(uri);
    if (response.statusCode == 200) {
      var json = response.body;
      postFromJson(json);
      return postFromJson(json); //Error here
    }
  }
}

这是解析后的 json

// To parse this JSON data, do
//
//     final post = postFromJson(jsonString);

import 'dart:convert';

Post postFromJson(String str) => Post.fromJson(json.decode(str));

String postToJson(Post data) => json.encode(data.toJson());

class Post {
    Post({
       required this.status,
       required this.totalResults,
       required this.articles,
    });

    String status;
    int totalResults;
    List<Article> articles;

    factory Post.fromJson(Map<String, dynamic> json) => Post(
        status: json["status"],
        totalResults: json["totalResults"],
        articles: List<Article>.from(json["articles"].map((x) => Article.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "status": status,
        "totalResults": totalResults,
        "articles": List<dynamic>.from(articles.map((x) => x.toJson())),
    };
}

class Article {
    Article({
       required this.source,
       required this.author,
       required this.title,
       required this.description,
       required this.url,
       required this.urlToImage,
       required this.publishedAt,
       required this.content,
    });

    Source source;
    String author;
    String title;
    String description;
    String url;
    String urlToImage;
    DateTime publishedAt;
    String content;

    factory Article.fromJson(Map<String, dynamic> json) => Article(
        source: Source.fromJson(json["source"]),
        author: json["author"] == null ? null : json["author"],
        title: json["title"],
        description: json["description"],
        url: json["url"],
        urlToImage: json["urlToImage"],
        publishedAt: DateTime.parse(json["publishedAt"]),
        content: json["content"],
    );

    Map<String, dynamic> toJson() => {
        "source": source.toJson(),
        "author": author == null ? null : author,
        "title": title,
        "description": description,
        "url": url,
        "urlToImage": urlToImage,
        "publishedAt": publishedAt.toIso8601String(),
        "content": content,
    };
}

class Source {
    Source({
        required this.id,
        required this.name,
    });

    String id;
    String name;

    factory Source.fromJson(Map<String, dynamic> json) => Source(
        id: json["id"] == null ? null : json["id"],
        name: json["name"],
    );

    Map<String, dynamic> toJson() => {
        "id": id == null ? null : id,
        "name": name,
    };
}

您的 return 类型 getPosts<List<String>?> 但您 return 是 Post 类型。我不知道您的 API 呼叫的响应是什么,但请尝试将 return 类型更改为 Post,例如 Future<Post?> getPosts().