我如何在 FutureBuilder 中使用多未来?

时间:2021-05-07 05:11:10

标签: flutter flutter-futurebuilder

开始异步调用该函数并多次调用。

但是,对第二个调用的响应始终为空。

我想在获得所有结果后将其更新到屏幕上。

有时会像这样调用错误消息:

“List”类型不是“Map”类型的子类型

class _HomePageState extends State<Home> {
  final _formKey = GlobalKey<FormState>();
  final _formKey2 = GlobalKey<FormState>();
  aboutRes about; // delete

  var _callStack = [Gateway.Instance.about(), Wifi.Instance.ssidInfo(0)];

   @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body : Padding(
        padding: const EdgeInsets.all(15.0),
        child: Column(children: [
          Expanded(
            child: FutureBuilder(
              future : Future.wait(_callStack),
              key : _formKey,
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  return Text('Invoked Error is ${snapshot.error}');

                } else if (snapshot.hasData) {
                  return ListView (
                    padding: const EdgeInsets.all(0.8),
                    children: [
                      Container(
                        child : Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            InformationItem(about: snapshot.data[0]),
                            WirelessItem(ssid: snapshot.data[1]),
                          ],
                        ),
                      )
                    ]
                  );
                } else {
                  return CircularProgressIndicator();
                }
              }
            )
          ),
        ]),
      )
    );
  }
}
  Future<aboutRes> about() async {
    aboutRes res;

    ApiResponse response = await get(GATEWAY_ABOUT);
    // for jsonSerialization
    Map resMap = jsonDecode(response.body);
    res = aboutRes.fromJson(resMap);
    return res;
  }

0 个答案:

没有答案