RangeError (index): Invalid value: Valid value range is empty: List Map 中为 0

RangeError (index): Invalid value: Valid value range is empty: 0 in List Map

在我的应用程序中,我想要一个带有饼图的 listView。这是我正在使用的 pie_chart 库。

但我在 List<Map<String,double>> 中遇到错误。

List<Map<String, double>> map = List<Map<String, double>>();

 ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          itemCount: data.length,
          itemBuilder: (context, index) {
            var item = data[index];
            map[index].putIfAbsent("< 1 Weeks ago>", () => (2.0));
            return PieChart(
                     ...
                dataMap: map[index],
                     ...
            )
    }

错误

RangeError (index): Invalid value: Valid value range is empty: 0

错误指向 map[index].putIfAbsent("< 1 Weeks ago>", () => (2.0));。这里有什么问题?

要从数据中添加每个元素并将它们添加到地图中,我这样做了:

  @override
  Widget build(BuildContext context) {
    List map = [];

    List data = [
      {'entry_1': 1},
      {'entry_2': 2},
      {'entry_3': 3}
    ];

    return MaterialApp(
      title: MyApp._title,
      home: Scaffold(
        body: ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          itemCount: data.length,
          itemBuilder: (context, index) {
            var item = data[index];
            map.add(item);
            return Container(
              height: 100,
              child: Text('Entry: ${map[index]}'),
            );
          },
        ),
      ),
    );
  }

这获取地图“数据”列表,获取每个值并将其添加到列表“地图”,然后构建一个容器列表,其中包含一个文本小部件,其中包含地图从数据中获取的相应条目

根据@Jamie 的线索设法修复了它

ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          itemCount: analytic.unitOwnerByWorkOrderCount.length,
          itemBuilder: (context, index) {
            var item = data[index];
            Map<String, double> map = Map<String, double>();
            List<Map<String, double>> list = List<Map<String, double>>();

            map.putIfAbsent("< 1 Weeks ago>",
                () => (item['less_one_weeks_ago'].toDouble()));
            list.add(map);
                 ....

           return PieChart(
               dataMap: list[index];
                 ....
           )
  }