Home > Net >  A value type 'post' can't be returned from the method getPosts because it has a retur
A value type 'post' can't be returned from the method getPosts because it has a retur

Time:04-12

Hi guy's I am having the error A value type 'post' can't be returned from the method getPosts because it has a return type of Future<List?> when trying to connect api's in flutter does anyone know how I can fix this? It was working when I was using some other dummy json data but when I switched to the newsapi I started getting the error.

Here is the services page

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
    }
  }
}

Here is the parsed 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,
    };
}

CodePudding user response:

Your return type of getPosts is <List<String>?> but you are returning a Post type. I don't know what is the response of your API call, but try changing the return type to Post eg Future<Post?> getPosts().

  • Related