我得到这个错误'type'Future'不是'ApplicationMeta'类型的子类型。我创建了一个支付功能。支付功能类型是future<列表>。此函数返回列表中的installedUpiapps。我需要将一个参数传递给appWidget。但是我收到此错误"类型"Future"不是类型"ApplicationMeta"的子类型。"。
import 'package:flutter/material.dart';
import 'package:upi_pay/upi_pay.dart';
class Homes extends StatelessWidget {
payment() async {
var appMetaList = await UpiPay.getInstalledUpiApplications();
return appMetaList;
}
Widget appWidget(ApplicationMeta appMeta) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
appMeta.iconImage(48), // Logo
Container(
margin: EdgeInsets.only(top: 4),
alignment: Alignment.center,
child: Text(
appMeta.upiApplication.getAppName(),
textAlign: TextAlign.center,
),
),
],
);
}
@override
Widget build(BuildContext context) {
ApplicationMeta as = payment();
return appWidget(as);
}
}
向payment
方法添加返回类型后,应该会看到编译错误。
payment
是async
,所以它返回一个Future
。为了用它构建一个小部件,您必须使用FutureBuilder
,如下所述https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
FutureBuilder<List<ApplicationMeta>>(
future: as
builder: (context, snapshot) {
// your widgets go here
}
)
关于显示ApplicationMeta
的列表,请查看以下内容:
- https://flutter.dev/docs/cookbook/lists/basic-list
- https://stackoverflow.com/a/54164017/10210069