由于分析带有列表的嵌套结构,从future返回数据时类型为False



我是新手,只接触过PHP,所以你可能会觉得我的问题毫无意义。如果是的话,请向我解释我做错了什么,谢谢。我使用两个项目作为我的项目来源:https://github.com/abdl-slm/FlutterGridFutureExample和https://github.com/PoojaB26/ParsingJSON-Flutter。

我正在尝试使用Future生成器和grid_view修改API的get数据,但当我尝试从Future返回列表时,我收到了以下消息:

不能从函数返回"ActionsList"类型的值"getActions",因为它的返回类型为'未来<列出>'。

我已经测试过,将数据解析到ActionsList没有问题(我可以在未来打印它们(,但我无法将它们发送到futurebuilder的其他部分。

未来是这样的:

Future<List<ActionsList>> getActions() async {
final response = await http.get(
"https://www.myproject.cz/api/...",
headers: {
"contentType": "application/x-www-form-urlencoded",
"Authorization": "Bearer ...",
},
);
if (response.statusCode == 200) {
Map<String, dynamic> decodedActions = json.decode(response.body);
String encodedActions = jsonEncode(decodedActions['actions_data']);
final jsonResponse = json.decode(encodedActions);
ActionsList actionsList = ActionsList.fromJson(jsonResponse);
print("got data from API check: " + actionsList.actions[0].name);
return actionsList;
} else {
throw Exception('Failed to load actions');
}
}

API的JSON是:

{
"actions_data": [
{
"id": "245629634987701888",
"shop_id": "182980954485185472",
"name": "Slevový kupón 10 % na techniku na FotoŠkoda",
"valid_from": "2020-11-08T00:00:00+00:00",
"valid_to": null,
"description": "",
"type": "coupon",
"other_type": null,
"discount": "10",
"discount_type": "%",
"discount_coupon": "POH10",
"image_mobile_url": null,
"aggregator": "affiliate_port",
"redirect_link": "https://project.cz/aff_c?offer_id=234&aff_id=5921&aff_sub=107553508877665280"
},
{
"id": "245628934350208704",
"shop_id": "180719630782866560",
"name": "Slevový kupon 200 Kč na dioptrické brýle na Alensa",
"valid_from": "2020-11-08T00:00:00+00:00",
"valid_to": null,
"description": "",
"type": "coupon",
"other_type": null,
"discount": "200",
"discount_type": "CZK",
"discount_coupon": "dioptricke200",
"image_mobile_url": null,
"aggregator": "ehub",
"redirect_link": "https://project.cz/click.php?a_aid=bb9f4d85&a_bid=acf4b661&data1=107553508877665280"
},
{
"id": "243716688296602880",
"shop_id": "194864134897055232",
"name": "Sleva na nábytek až 20%",
"valid_from": "2020-11-03T00:00:00+00:00",
"valid_to": null,
"description": "",
"type": "coupon",
"other_type": null,
"discount": "10",
"discount_type": "%",
"discount_coupon": "20AUTUMN",
"image_mobile_url": "https://www.project.cz/actions-images/243716688296602880-mobile-699018679.png",
"aggregator": "dognet",
"redirect_link": "http://www.project.cz/sd?data1=107553508877665280"
},...

映射数据的ActionList类:

import 'dart:convert';
class ActionsList {
final List<ActionsData> actions;
ActionsList({
this.actions,
});
factory ActionsList.fromJson(List<dynamic> parsedJson) {
List<ActionsData> actions = new List<ActionsData>();
actions = parsedJson.map((i)=>ActionsData.fromJson(i)).toList();
return new ActionsList(
actions: actions
);
}
}

class ActionsData {
final String id;
final String shopId;
final String name;
final String validFrom;
final String validTo;
final String description;
final String type;
final String otherType;
final String discount;
final String discountType;
final String discountCoupon;
final String imageMobileUrl;
final String aggregator;
final String redirectLink;
ActionsData(
{this.id,
this.shopId,
this.name,
this.validFrom,
this.validTo,
this.description,
this.type,
this.otherType,
this.discount,
this.discountType,
this.discountCoupon,
this.imageMobileUrl,
this.aggregator,
this.redirectLink});
Map<String, dynamic> toJson(){
return {
"id": this.id,
"shopId": this.shopId,
"name": this.name,
"validFrom": this.validFrom,
"validTo": this.validTo,
"description": this.description,
"type": this.type,
"otherType": this.otherType,
"discount": this.discount,
"discountType": this.discountType,
"discountCoupon": this.discountCoupon,
"imageMobileUrl": this.imageMobileUrl,
"aggregator": this.aggregator,
"redirectLink": this.redirectLink
};
}
factory ActionsData.fromJson(Map<String, dynamic> json){
return new ActionsData(
id: json['id'],
shopId: json['shop_id'],
name: json['name'],
validFrom: json['valid_from'],
validTo: json['valid_to'],
description: json['description'],
type: json['type'],
otherType: json['other_type'],
discount: json['discount'],
discountType: json['discount_type'],
discountCoupon: json['discount_coupon'],
imageMobileUrl: json['image_mobile_url'],
aggregator: json['aggregator'],
redirectLink: json['redirect_link']);
}
}

主屏幕的其余部分:

class ThirdScreen extends StatefulWidget {
ThirdScreen({Key key, this.title}) : super(key: key);
final String title;
@override
_ThirdScreenState createState() => _ThirdScreenState();
}
class _ThirdScreenState extends State<ThirdScreen> with TickerProviderStateMixin {
Future<List<ActionsList>> futureAction;

@override
void initState() {
super.initState();
futureAction = getActions();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
titleSpacing: 0.0,
title: IconButton(
icon: Icon(
Icons.shopping_cart,
),
onPressed: () {},
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.menu,
),
onPressed: () {},
)
],
),
body: Center(
child: FutureBuilder<List<ActionsList>>(
future: futureAction,
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
print("snapshot doesn't have data");
return Center(child: CircularProgressIndicator());
} else {
// print(snapshot.data);
return Container(child: _albumGridView(snapshot.data));
}
},
)),
);
}
}

感谢您的任何提示或样品!

Future方法不会返回任何内容,除非您在else-if语句之外返回值。将您的代码更改为:

Future<List<ActionsList>> getActions() async {
ActionsList actionsList;
final response = await http.get(
"https://www.myproject.cz/api/...",
headers: {
"contentType": "application/x-www-form-urlencoded",
"Authorization": "Bearer ...",
},
);
if (response.statusCode == 200) {
Map<String, dynamic> decodedActions = json.decode(response.body);
String encodedActions = jsonEncode(decodedActions['actions_data']);
final jsonResponse = json.decode(encodedActions);
actionsList = ActionsList.fromJson(jsonResponse);
print("got data from API check: " + actionsList.actions[0].name);
} else {
throw Exception('Failed to load actions');
}
return actionsList;
}

希望这对你有用。

Future<ActionsList>() async {
final response = await http.get(
"https://www.myproject.cz/api/...",
headers: {
"contentType": "application/x-www-form-urlencoded",
"Authorization": "Bearer ...",
},
);
if (response.statusCode == 200) {
Map<String, dynamic> decodedActions = json.decode(response.body);
String encodedActions = jsonEncode(decodedActions['actions_data']);
final jsonResponse = json.decode(encodedActions);
ActionsList actionsList = ActionsList.fromJson(jsonResponse);
print("got data from API check: " + actionsList.actions[0].name);
return actionsList;
} else {
throw Exception('Failed to load actions');
}
}

将函数的返回类型从"更改为";未来<列表<操作列表>gt"至";未来<操作列表>quot;它会解决你的问题。

最新更新