首页 > 解决方案 > 颤振条件渲染。不工作?

问题描述

颤振条件渲染。不工作?。我确实尝试了很多方法,但我找不到任何解决方案。我想在data[index].status 不为空时渲染一个小部件。我的代码对我不起作用。

现在我遇到了另一个问题元素类型“Set”不能分配给列表类型“Widget”。 有我的代码的图像

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFecf0f1),
      appBar: AppBar(
        actions: <Widget>[],
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: ListView.builder(
              padding: EdgeInsets.all(10),
              itemCount: data.length,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  margin: EdgeInsets.all(10),
                  child: Padding(
                    padding: EdgeInsets.all(5),
                    child: Container(
                      child: Column(
                        children: <Widget>[
                          Container(
                            child: Row(
                              children: <Widget>[
                                Container(
                                  child: Text(data[index].airline,
                                      style: TextStyle(
                                        fontFamily: '',
                                        fontSize: 20,
                                        color: Colors.black,
                                        fontWeight: FontWeight.w300,
                                      )),
                                ),
                                if (data[index].status == null)
                                  {
                                    Container(
                                      child: Text('somthing'),
                                    )
                                  }
                                else
                                  {
                                    Container(
                                      decoration: BoxDecoration(
                                          color: Colors.red[700],
                                          borderRadius:
                                              BorderRadius.circular(20)),
                                      padding: EdgeInsets.symmetric(
                                          vertical: 6, horizontal: 8),
                                      child: Text(
                                        data[index].status,
                                        style: TextStyle(
                                            fontSize: 15,
                                            color: Colors.white,
                                            fontWeight: FontWeight.normal,
                                            fontFamily: 'Raleway'),
                                      ),
                                    ),
                                  }
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}



标签: flutterdart

解决方案


集合 ifif将/子句之后的花括号解释else为不是代码块,而是set文字:

此代码编译正常:

class GoodWidget extends StatelessWidget {
  final bye = false;
  @override
  Widget build(BuildContext context) {
    return Column(children: [
      if (bye) Text('Goodbye') else Text('Hello'),
      Text('World!')
    ]);
  }
}

这段代码会产生错误 The element type 'Set<Container>' can't be assigned to the list type 'Widget'

class BadWidget extends StatelessWidget {
  final bye = false;
  @override
  Widget build(BuildContext context) {
    return Column(children: [
      if (bye) {Text('Goodbye')} else {Text('Hello')},
      Text('World!')
    ]);
  }
}

在这种情况下,不允许使用大括号。


推荐阅读