颤振条件渲染。无法正常工作?

时间:2020-04-05 17:58:31

标签: flutter dart

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

现在我又遇到了其他问题 元素类型“设置”不能分配给列表类型“小部件”。 There is the image of my code

@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'),
                                      ),
                                    ),
                                  }
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}



2 个答案:

答案 0 :(得分:0)

您正在将data[index].status 的值设置为null。

您使用的是=,这意味着使用一个值而不是等价支票的==

您的代码应为

Row(

    children:<widget>[
      if (data[index].status == null) // note the ==
                         {
                              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'),
                                          ),
                                       ),
                                      }

    ]
    )

Image to help with Operators - Taken from reference(1)

参考文献

  1. Dart: Operators - Nice to read

答案 1 :(得分:0)

collection if解释if / 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!')
    ]);
  }
}

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