【问题标题】:Get other Futures data even if one Future throws error with Future.wait即使一个 Future 使用 Future.wait 抛出错误,也可以获取其他 Futures 数据
【发布时间】:2021-12-03 07:27:13
【问题描述】:

我想一口气从多个 Futures 中提取。所以我使用 Future.wait。但是如果 Future1 抛出错误,我将无法获取 Future2 的值。

有没有 Future.wait 的解决方案?

FutureBuilder(
                  future: Future.wait([Future1,Future2]),
                  builder: (context, snapshot) {
                    switch (snapshot.connectionState) {
                      case ConnectionState.none:
    
                      case ConnectionState.active:
    
                      case ConnectionState.waiting:
                        return SizedBox(height: 600, child: LoadingIndicator());
                      case ConnectionState.done:
                       // snapshot.data will come as null because there is an error
                        
                        ...

【问题讨论】:

标签: flutter dart


【解决方案1】:

使用 catchError:

Future.wait([
  // fake future 1
  Future.delayed(const Duration()).catchError((e) {
    // add your error handler
  }),
  // fake future 2
  Future.delayed(const Duration()).catchError((e) {
    // add your error handler
  }),
]);

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2015-01-01
    • 2021-11-03
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多