Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// 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,
    };
}

>Solution :

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().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading