您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

遍历列表以在Flutter中呈现多个小部件?

遍历列表以在Flutter中呈现多个小部件?

基本上,当您在函数上单击“ return”时,该函数将停止并且不会继续进行迭代,因此您需要做的是将其全部放入列表中,然后将其添加为小部件的子级

您可以执行以下操作:

  Widget getTextWidgets(List<String> strings)
  {
    List<Widget> list = new List<Widget>();
    for(var i = 0; i < strings.length; i++){
        list.add(new Text(strings[i]));
    }
    return new Row(children: list);
  }

甚至更好,您可以使用.map()运算符并执行以下操作:

  Widget getTextWidgets(List<String> strings)
  {
    return new Row(children: strings.map((item) => new Text(item)).toList());
  }
其他 2022/1/1 18:17:02 有524人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶