根据对象列表生成新卡 [英] Generating new Cards based on List of Objects

查看:94
本文介绍了根据对象列表生成新卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象列表,有没有一种方法可以遍历Card小部件,以便对于此列表中的每个对象,使用该小部件的属性创建一个新卡.

I have a list of objects, is there a way to loop over a Card widget so that for each object in this list, a new card created with the attributes of this widget of interest.

以下代码绝对不起作用,它仅是为了说明我要实现的目标背后的逻辑

The following code definitely does not work, it only serves as an illustration to show the logic behind what I am trying to achieve

//function declaration and body...
List<CardInfo> list = new List();
for (var i in cardInfo){ //cardInfo is a list of json created previously.
list.add(new CardInfo.fromJson(i));
    print (list); }//tested the list and it contains the objects I want
 return list;
}

...

//my widget{
    @override
      Widget build(BuildContext context) {
        return new Dismissible(
for (i in list){
            child:  new Card(
              child:
              new ListTile(leading: new Icon(Icons.star, color: Colors.yellowAccent,),
                title: new Text(list(i).field1),//field1 is a string in CardInfo
                subtitle: new Text(list(i).field2),//field2 is a string in CardInfo

这是可以做到的吗?

更新

我想出了将每个JSON转换为对象,然后直接使用这些对象的方法,现在_loadData()函数在每次迭代时都返回一个对象

I figured out converting each JSON into object and then working with the objects directly, now _loadData() function returns an object on each iteration

_loadData() async {
  var url = 'myurl';
  var httpClient  = createHttpClient();
  var response =await httpClient.get(url);
  List cardInfo = JSON.decode(response.body);
  List<CardInfo> list = new List();
  for ( var i in cardInfo){
    var ci = new CardInfo.fromJson(i);

    list.add(new CardInfo.fromJson(i));

  return ci;
  }
  //return list;

}


....

有没有办法设置我的类构造函数,以便它允许我访问返回对象的字段?像这样的东西:

So is there a way to set up my class constructor so that it allows me to access the fields of the returned objects ? something like this:

title: new Text(new CardInfo(_loadData()).field)

更新2:

@override
    Widget build(BuildContext context) {
      return new FutureBuilder(
          future: _loadData(),
          builder: (BuildContext context, AsyncSnapshot<List> jsonList){
            if (jsonList.hasData==true){
              var cardInfo = jsonList.data[0];//is this right ?
              print (cardInfo); 
              return new Dismissible(
                child: new Column(
                  children:
                  cardInfo.map(
                          (CardInfo info)=>
                      new Card(
                          child: new ListTile(leading: new Icon(Icons.star, color: Colors.yellowAccent,),
                            title: new Text(info.field),
                            subtitle: new Text("Subtitle"),)

                      ) ), ),....

我的功能位于顶部,如下所示:

My function is at the top as follows:

  _loadData() async {
var url = 'myurl';
var httpClient = createHttpClient();
var response = await httpClient.get(url);
List cardInfo = JSON.decode(response.body);


List<CardInfo> list = new List();
for (var i in cardInfo) {


list.add(new CardInfo.fromJson(i));
}


return list;
}

我收到以下错误:

"CardInfo"类没有实例方法"map".

Class 'CardInfo' has no instance method 'map'.

推荐答案

您可以使用

You can use the Iterable.map method to do this. For example:

new Column(
  children: list.map(
    (CardInfo info) => new Card(child: new Text(info.field1)),
  ).toList()
)

这篇关于根据对象列表生成新卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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