无法从函数"fetchPromotions"返回"Resut"类型的值,因为它的返回类型为Future< List< Promotions>>. [英] A value of type 'Resut' can't be returned from function'fetchPromotions' because it has a return type of Future<List<Promotions>>

查看:63
本文介绍了无法从函数"fetchPromotions"返回"Resut"类型的值,因为它的返回类型为Future< List< Promotions>>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从API中获取一些数据,该API返回一个Json数组,promotions_model.dart会完成所有解析,但是此错误正在显示.

I am fetching some data from an API, which returns a Json array, promotions_model.dart does all the parsing, but this error is showing up.

错误-无法从函数"fetchPromotions"返回结果"类型的值,因为其返回类型为"Future< List>".

Error-- A value of type 'Result' can't be returned from function 'fetchPromotions' because it has a return type of 'Future<List>'.

有人可以告诉我我在这里做错了吗.谢谢

can someone please tell me what i am doing wrong here. thanks

**promotions_model.dart**
import 'dart:convert';

Result resultFromJson(String str) => Result.fromJson(json.decode(str));

String resultToJson(Result data) => json.encode(data.toJson());

class Result {
  Result({
    this.code,
    this.result,
  });

  final int code;
  final List<Promotions> result;

  factory Result.fromJson(Map<String, dynamic> json) => Result(
        code: json["Code"],
        result: List<Promotions>.from(
            json["Result"].map((x) => Promotions.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "Code": code,
        "Result": List<dynamic>.from(result.map((x) => x.toJson())),
      };
}

class Promotions {
  Promotions({
    this.id,
    this.title,
    this.description,
    this.image,
  });

  final String id;
  final String title;
  final String description;
  final String image;

  factory Promotions.fromJson(Map<String, dynamic> json) => Promotions(
        id: json["id"],
        title: json["title"],
        description: json["description"],
        image: json["image"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "description": description,
        "image": image,
      };
}


**promotion-api.dart**
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:project/models/promotions_model.dart';

const key = {
  'APP-X-RESTAPI-KEY': "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
};

const API = 'http://111.111.11.1/project';

Future<List<Promotions>> fetchPromotions() async {
  final response = await http.get(API + '/promotion/all', headers: key);

  if (response.statusCode == 200) {
    return resultFromJson(response.body); // This line is causing the error
  } else {
    print(response.statusCode);
  }
}

推荐答案

错误清楚地说明了这一点.它需要 Result 作为返回类型.你可以这样,

The Error says it clearly. It needs Result as the return type. You can something like this,

Result fetchPromotions() async {
  final response = await http.get(API + '/promotion/all', headers: key);
  Result result = null;
  if (response.statusCode == 200) {
    result = resultFromJson(response.body); // This line is causing the error
  } else {
    print(response.statusCode);
  }
  return result;
}

希望你有个主意.

这篇关于无法从函数"fetchPromotions"返回"Resut"类型的值,因为它的返回类型为Future&lt; List&lt; Promotions&gt;&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆