【问题标题】:Flutter : how to conditionally add a widgetFlutter:如何有条件地添加小部件
【发布时间】:2019-11-26 06:10:52
【问题描述】:

如果没有返回数据,我想在正文中添加一个文本小部件。否则,我想显示数据。但是,返回以下错误:

flutter: type '_CompactLinkedHashSet<Widget>' is not a subtype of type 'Widget'

我的代码:

body: SafeArea(
  child: (isLoading)
    ? Center(
         child: new CircularProgressIndicator(),
      )
    : Stack(
         children: <Widget>[
            (jobDetail == null && !isLoading)
                ? noDataFound()
                : {
                     detailWidget(context, jobDetail),
                      applyWidget(context, jobDetail)
                  }
          ],
      ),
),

这是我的文本小部件代码

Widget noDataFound() {
    return Container(
      child: Center(
          child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(
          "We could not get the detail. Please try again later",
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 20.0),
        ),
      )),
    );
  }

【问题讨论】:

  • 你尝试过未来的建设者吗?
  • 谢谢。不,我没有尝试过未来的建设者。我目前的逻辑有什么不对吗?
  • 尝试删除这个:&& !isLoading

标签: flutter


【解决方案1】:

如错误消息所述,您正试图将一组小部件的散列分配给堆栈({...} 部分使其成为散列集)。最简单的解决方法是更改​​三元运算符的位置。

例子简化为核心问题:

Stack(
  children: (jobDetail == null && !isLoading)
    ? [
        Container(),
      ]
    : [
        Container(),
        Container(),
      ],
),

【讨论】:

    猜你喜欢
    • 2019-02-03
    • 2021-07-24
    • 1970-01-01
    • 2021-05-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多