简体   繁体   English

如何在Flutter中创建对象实例的可迭代集合?

[英]How do I create an iterable collection of object instances in Flutter?

I have created a list of myObject Type, When I add an object instance via list.add then passing the constructor for myObject , the first property I can reference, subsequent properties are null. 我创建了一个myObject类型的列表,当我通过list.add添加对象实例,然后传递myObject的构造函数时,我可以引用的第一个属性,后续属性为null。

// my class and its constructor method

class Channel {
  final String channelName;
  final String channelDesc;
  final String channelEvent;
  final String channelThumbLink;

  const Channel(this.channelName, this.channelDesc, this.channelEvent,
      this.channelThumbLink);
}

// I construct a list of type<Channel>

final List<Channel> channelList = <Channel>[]
    ..add(Channel(
        'Funkycooking',
        'Welcome to the Funkycooking channel, we cook, you get funky',
        'Sizzling Salsa Saturday!',
        '1'))

// Referencing these elements from the list collection in any widget

Text('${channelList[0].channelName}'),
Text('${channelList[0].channelDesc}'),

Text('${channelList[0].channelName}') returns 'Funkycooking' as expected Text('${channelList[0].channelName}')返回'Funkycooking'

Text('${channelList[0].channelDesc}') returns null, i would expect 'Welcome to the Funkycooking channel, we cook, you get funky' Text('${channelList[0].channelDesc}')返回null,我希望“欢迎来到Funkycooking频道,我们做饭,您会变得时髦”

抱歉,我错误地认为热重装可以正确反映我的对象编码更改,但不能,但是通过停止并重新启动应用程序,它的行为符合我的预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM