如何将任何可迭代对象分配给小部件?

How can I assign any iterable to a widget?

错误:无法将元素类型 'List' 分配给列表类型 'Widget'。

I am learning to make flutter apps and this same code is illustrated by other person who is teaching me in that video and it works fine. Maybe its a version problem.

return Card(
      margin: EdgeInsets.all(10),
      child: Column(
        children: [
          ListTile(
            title: Text('₹${widget.order.amount}'),
            subtitle: Text(DateFormat('dd/MM/yyyy hh:mm').format(widget.order.dateTime)),
            trailing: IconButton(icon: Icon(_expanded ? Icons.expand_less :Icons.expand_more),
                onPressed: (){
              setState(() {
                _expanded = !_expanded;
              });
                }),
          ),
          if(_expanded)
            Container(
              height: min(widget.order.products.length * 20.0 + 10, 100),
              child: ListView(
                children: [
                  widget.order.products.map((prod) => Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(prod.title,
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold
                        ),
                      ),
                      Text('₹${prod.quantity}x ₹${prod.price}',
                        style: TextStyle(
                            fontSize: 18,
                            color: Colors.grey
                        ),
                      )
                    ],
                  ),).toList()
                ],
              ),
            )
        ],
      ),
    );

你应该在映射时删除 [ ],检查下面

ListView(
    children: widget.order.products.map((prod) => Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
            Text(
               prod.title,
               style: TextStyle(
                 fontSize: 18,
                 fontWeight: FontWeight.bold
               ),
            ),
            Text(
               '₹${prod.quantity}x ₹${prod.price}',
                style: TextStyle(
                   fontSize: 18,
                   color: Colors.grey
                ),
            )
                    ],
        ),).toList(),
),

只需删除 ListView

的 children 的 []

您的代码应如下所示:

return Card(
      margin: EdgeInsets.all(10),
      child: Column(
        children: [
          ListTile(
            title: Text('₹${widget.order.amount}'),
            subtitle: Text(DateFormat('dd/MM/yyyy hh:mm').format(widget.order.dateTime)),
            trailing: IconButton(icon: Icon(_expanded ? Icons.expand_less :Icons.expand_more),
                onPressed: (){
              setState(() {
                _expanded = !_expanded;
              });
                }),
          ),
          if(_expanded)
            Container(
              height: min(widget.order.products.length * 20.0 + 10, 100),
              child: ListView(
                children: 
                  widget.order.products.map((prod) => Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(prod.title,
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold
                        ),
                      ),
                      Text('₹${prod.quantity}x ₹${prod.price}',
                        style: TextStyle(
                            fontSize: 18,
                            color: Colors.grey
                        ),
                      )
                    ],
                  ),).toList()
                ,
              ),
            )
        ],
      ),
    );

因为您已经在 .toList() 中创建了小部件列表,所以它不应该被 []

包围

ListView 小部件需要 'List of Widgets',但是,您在使用地图时为其提供了嵌套的小部件列表,并在将它们转换为列表时返回 'Rows'。因此,简而言之,ListView 在另一个列表中呈现行列表。 您可以使用 'Spread Operator' 来解决问题。它将获取嵌套列表中的每个行小部件并将其作为 'ListView' 的直接子项放置。 在映射产品之前使用三个点“...”将解决错误。像这样; “...widget.order.products.map((产品) => 行(” 要么 只需删除 ListView 的子项的 []。它会做同样的事情。