flutter始めたばかりで1時間ほどタイトルのエラーではまりました

とりあえず結論

// ダメ
Container(
  height: min(widget.order.products.length * 20.0 + 10, 180),
  child: ListView(
    children: widget.order.products.map((prod) => Row()),
  ),
),

// OK
// RowにtoList()メソッド入れる
Container(
  height: min(widget.order.products.length * 20.0 + 10, 180),
  child: ListView(
    children: widget.order.products.map((prod) => Row()).toList(), 
  ),
),

なぜ発生していたか?

Rowのchildrenには、通常<Widget>[]が入っており、Widget型のリストが期待されています。

しかしmapではRow だけ返していて、List型に変換されていないことが問題でした。

なので、最後にtoList() メソッドを入れてあげることで、<Widget>[] に適した型となり、エラーを吐き出さないようになります。

参考記事

toList method

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA