簡體   English   中英

在 Column 小部件下編寫三元運算符時出現錯誤。 這實際上可以完成,我該如何解決?

[英]I am getting an error while writing a ternary operator under a Column widget. Can this actually be done and how do i fix it?

我有一個數組,其中包含一個圖像 URL 和普通字符串作為其內容。 我想根據正在讀取的值顯示圖像或文本小部件。 我用它來完成這項工作:

Widget build(BuildContext context) {
    final usrMap = {"tom", 'tom.png', "taommy"};
    return MaterialApp(
      title: 'Flutter Tutorial',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Text Widget Tutorial'),
        ),
        body: Column(
          children: [
            for( var prop in usrMap){
              prop.contains(".com")? Text(prop) : Text("not .png")
            },
        ]
         ),
      ),
    );
  }

我收到此錯誤無法將元素類型“Set”分配給列表類型“Widget”如何解決此問題?

像這樣使用。

 Widget build(BuildContext context) {
    final usrMap = {
      "tom",
      'tom.png',
      "taommy",
      "sheikh.com",
    };
    return MaterialApp(
      title: 'Flutter Tutorial',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Text Widget Tutorial'),
        ),
        body: Column(children: [
          ...usrMap.map((e) {
            if (e.contains(".com"))
              return Text("prop");
            else
              return Text("not .png");
          }).toList(),
        ]),
      ),
    );
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM