颤振中的扩展

时间:2018-08-27 01:37:24

标签: listview flutter expandablelistview

我正在尝试从后端获取的数据构建扩展图块,而后端获取的数据看起来像

var sampleresponse = [ 
   {"Total Income": 500.0},
   {"Total Expense": 1000.0},
   {"Maintenance": 500.0},
   {"Tank Water": 100.0, "truck Charges": 900.0},
];

var sampletotalIncome = sampleresponse[0];
var sampletotalExpense = sampleresponse[1];
var sampledetailIncome = sampleresponse[2];
var sampledetailExpense = sampleresponse[3];

var sampletotalIncomeKeys = sampletotalIncome.keys;
var sampletotalExpenseKeys = sampletotalExpense.keys;
var sampledetailIncomeKeys = sampledetailIncome.Keys;
var sampledetailExpenseKeys = sampledetailExpense.keys;

var sampletotalIncomevalue = sampletotalIncome.value;
var sampletotalExpensevalue = sampletotalExpense.value;
var sampledetailIncomevalue = sampledetailIncome.value;
var sampledetailExpensevalue= sampledetailExpense.value;

//sampletotalIncomeKeys is [Total Income] 
//sampletotalIncomevalue is [500.0] 
//sampletotalExpenseKeys is [Total Expense] 
//sampletotalExpensevalue is [1000.0] 
//sampledetailIncomeKeys is [Maintanence] 
//sampledetailIncomevalue is [500.0] 
//sampledetailExpenseKeys is [Tank Water, truck Charges] 
//sampledetailedExpensevalue is [100.0, 900.0]

其中“维护”:500.0应放置在“总收入”下,“总费用”应包含“储罐水”和“卡车费用”。这是我的代码:

ExpansionTile.dart中的代码段:

import 'package:flutter/material.dart';

class StuffInTiles extends StatelessWidget {
final MyTile myTile;

StuffInTiles(this.myTile);

@override
Widget build(BuildContext context) {
  return _buildTiles(myTile);
}

Widget _buildTiles(MyTile t) {
  if (t.children.isEmpty)
    return new ListTile(
      title: new Text(
        t.title,
        style: new TextStyle(color: Colors.white),
      ),
      trailing: new Text(
        t.value,
        style: new TextStyle(color: Colors.white),
      ),
    );
  return new ExpansionTile(
    key: new PageStorageKey<MyTile>(t),
    title: new Text(
      t.title,
      style: new TextStyle(color: Colors.white),
    ),
    trailing: new Text(
      t.value,
      style: new TextStyle(color: Colors.white),
    ),
    children: t.children.map(_buildTiles).toList(),
  );
}

}

class MyTile {
 final String title;
 final String value;
 final List<MyTile> children;

 MyTile(this.title, this.value, [this.children = const <MyTile>[]]);

}

}

Main.dart中的片段:

List<MyTile> itemList = <MyTile>[

          new MyTile(
            sampletotalIncomeKeys.toString(),
            sampletotalIncomevalue.toString(),
            sampledetailIncomeKeys
                .map(
                  (items) => new 
            MyTile(sampledetailIncomeKeys.toString(),
                      sampledetailIncomevalue.toString()),
                )
                .toList(),
          ),
          new MyTile(
            sampletotalExpenseKeys.toString(),
            sampletotalExpensevalue.toString(),
            sampledetailedExpense.map((items, value) => 
            MyTile(items.toString(), value.toString()).toString(),
              ).toString()

          ),
        ];

 ...........
new Expanded(
   child: new ListView.builder(
     itemBuilder: (BuildContext context, int index) {
       return new StuffInTiles(itemList[index]);
     },
   itemCount: itemList.length,
  ),

与此有关,我得到一个错误

“参数类型“ String”不能分配给参数类型List。我对此不太理解。请提供一些逻辑方面的帮助吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码在这部分上是错误的:

    List<MyTile> itemList = <MyTile>[

              new MyTile(
                sampletotalIncomeKeys.toString(),
                sampletotalIncomevalue.toString(),
                sampledetailIncomeKeys
                    .map(
                      (items) => new 
                MyTile(sampledetailIncomeKeys.toString(),
                          sampledetailIncomevalue.toString()),
                    )
                    .toList(),
              ),
              new MyTile(
                sampletotalExpenseKeys.toString(),
                sampletotalExpensevalue.toString(),
                sampledetailedExpense.map((items, value) => 
                MyTile(items.toString(), value.toString()).toString(),
                  ).toString()

              ),
            ];

第二个对象的最后一个参数错误,应为List而不是String,用以下代码替换:

          List<MyTile> itemList = <MyTile>[
            new MyTile(
              sampletotalIncomeKeys.toString(),
              sampletotalIncomevalue.toString(),
              sampledetailIncomeKeys
                  .map(
                    (items) => new MyTile(sampledetailIncomeKeys.toString(),
                        sampledetailIncomevalue.toString()),
                  )
                  .toList(),
            ),
            new MyTile(
            sampletotalExpenseKeys.toString(),
            sampletotalExpensevalue.toString(),
            sampledetailedExpense.keys
                .map(
                  (key) =>
                      MyTile(key, sampledetailedExpense[key].toString()),
                )
                .toList()),
         ];
        }