我是 Flutter 的新手,并且只有 PHP 方面的经验,因此您可能会觉得我的问题很荒谬。如果是这样,请解释一下我做错了什么,谢谢。我正在使用两个项目作为我的项目的源代码:https ://github.com/abdl-slm/FlutterGridFutureExample和https://github.com/PoojaB26/ParsingJSON-Flutter。
我正在尝试使用 Future builder 和 grid_view 修改从 API 获取数据,但是当我尝试从未来返回列表时收到此消息:
无法从函数“getActions”返回“ActionsList”类型的值,因为它的返回类型为“Future<List>”。
我已经测试过将数据解析到 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));
}
},
)),
);
}
}
感谢您提供任何提示或样本!