【问题标题】:Is there a way of rendering a widget conditionally without adding an else statement? [duplicate]有没有办法在不添加 else 语句的情况下有条件地呈现小部件? [复制]
【发布时间】:2021-08-17 12:45:46
【问题描述】:

我只想在给定条件为真时才呈现列的子项。 我只知道使用三元运算符这样做。 我的问题是,如果不满足条件,您必须指定一个 else 块。 Flutter 是否提供了另一种类似于 React 的条件渲染的条件渲染方式?

反应示例

{unreadMessages.length > 0 &&
    <h2>
      You have {unreadMessages.length} unread messages.
    </h2>
  }

颤振问题

Column(children: [
                  true ? Container() : Text('Hello'), // remove else block
                ],),

【问题讨论】:

    标签: flutter


    【解决方案1】:
    Column(children:[
    if(unreadMessages.length > 0) Text('This will render only if length is > 0')
    ])
    

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2020-05-09
      • 2021-01-21
      • 1970-01-01
      • 2018-10-25
      • 2019-10-01
      • 1970-01-01
      • 2022-07-06
      相关资源
      最近更新 更多