如何创建返回小部件数组的函数 [英] How to create function which return array of widgets

查看:63
本文介绍了如何创建返回小部件数组的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个返回小部件数组的函数,如下所示:

I have to create a function which returns array of widgets, like this:

new GridView.count(crossAxisCount: _column,children: getRandomWidgetArray(),

函数示例如下:-

Widget[] getRandomWidgetArray()
    {
      Widget[] gameCells;
      for(i=0;i<5;i++)
       {
          gameCells.add(new MyCellWidget(i));
       }
      return gameCells;
    }

以上代码给出此错误:

我知道如何不使用功能:

I know how to do it without function:

children: < Widget >[ new MyCellWidget(0),
                      new MyCellWidget(1),]

但是我必须使其具有动态功能,因为将来值会改变,以上代码只是原型.颤抖的例子很少.

But I have to make it dynamic with function as values will change in future, above code is just prototype. Flutter examples are very few.

推荐答案

数组类型在Dart中为List<Type>:

Array types are List<Type> in Dart:

List<Widget> getRandomWidgetArray()

[]只能用作文字值来创建新的列表值,而不能用于类型声明.

[] can only be used as literal value to create a new list value, not for type declaration.

这篇关于如何创建返回小部件数组的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆