Plotting multi-dimensional array values in Flutter FlCharts

I’m trying to plot the array received from an API into FlCharts but I’m not able to correctly iterate over the second level.

My array is as follows. I’m currently testing with static sample values which I’ll replace from actual response later. I have full control over the API that’s sending the values so please suggest if I should optimize the array structure.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> var listData = [
{
'Product01': [12.0, 14.5, 15.6, 12.1, 15.2, 17.6, 12.8, 24.0, 35.4, 12.9]
},
{
'Product02': [9.0, 12.5, 11.6, 7.1, 10.2, 14.6, 8.8, 18.0, 31.4, 10.9]
},
];
</code>
<code> var listData = [ { 'Product01': [12.0, 14.5, 15.6, 12.1, 15.2, 17.6, 12.8, 24.0, 35.4, 12.9] }, { 'Product02': [9.0, 12.5, 11.6, 7.1, 10.2, 14.6, 8.8, 18.0, 31.4, 10.9] }, ]; </code>
 var listData = [
    {
      'Product01': [12.0, 14.5, 15.6, 12.1, 15.2, 17.6, 12.8, 24.0, 35.4, 12.9]
    },
    {
      'Product02': [9.0, 12.5, 11.6, 7.1, 10.2, 14.6, 8.8, 18.0, 31.4, 10.9]
    },
  ];

The code I’m trying is as below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var sections = listData.asMap().entries.map((data) {
final idx = data.key;
final listval = data.value;
return LineChartBarData(
spots: listval.asMap().entries.map((item) {
FlSpot(item.key, item.val);
}).toList(),
color: chartColors[idx],
barWidth: 5,
isCurved: true,
dotData: FlDotData(show: true),
);
}).toList();
</code>
<code>var sections = listData.asMap().entries.map((data) { final idx = data.key; final listval = data.value; return LineChartBarData( spots: listval.asMap().entries.map((item) { FlSpot(item.key, item.val); }).toList(), color: chartColors[idx], barWidth: 5, isCurved: true, dotData: FlDotData(show: true), ); }).toList(); </code>
var sections = listData.asMap().entries.map((data) {
      final idx = data.key;
      final listval = data.value;

      return LineChartBarData(
        spots: listval.asMap().entries.map((item) {
          FlSpot(item.key, item.val);
        }).toList(),
        color: chartColors[idx],
        barWidth: 5,
        isCurved: true,
        dotData: FlDotData(show: true),
      );
    }).toList();

and in the chart view

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>AspectRatio(
aspectRatio: 1.30,
child: LineChart(
LineChartData(
lineBarsData: sections,
),
</code>
<code>AspectRatio( aspectRatio: 1.30, child: LineChart( LineChartData( lineBarsData: sections, ), </code>
AspectRatio(
   aspectRatio: 1.30,
         child: LineChart(
                  LineChartData(
                    lineBarsData: sections,
              ),

The error I get is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lib/modules/home/widgets/yearly_expenses.dart:106:24: Error: The method 'asMap' isn't defined for the class 'Map<String, List<double>>'.
- 'Map' is from 'dart:core'.
- 'List' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'asMap'.
spots: listval.asMap().entries.map((item) {
^^^^^
</code>
<code>lib/modules/home/widgets/yearly_expenses.dart:106:24: Error: The method 'asMap' isn't defined for the class 'Map<String, List<double>>'. - 'Map' is from 'dart:core'. - 'List' is from 'dart:core'. Try correcting the name to the name of an existing method, or defining a method named 'asMap'. spots: listval.asMap().entries.map((item) { ^^^^^ </code>
lib/modules/home/widgets/yearly_expenses.dart:106:24: Error: The method 'asMap' isn't defined for the class 'Map<String, List<double>>'.
 - 'Map' is from 'dart:core'.
 - 'List' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'asMap'.
        spots: listval.asMap().entries.map((item) {
                       ^^^^^

I tried several variations (map, forEach) but none seems to work. I’m totally new to Dart coming from PHP. Any help is appreciated. Thanks in advance.

Edit: I updated my code as below, still get errors.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List sections = listData.asMap().entries.map((data) {
final idx = data.key;
final listval = data.value;
listval.entries.map((entry) {
final k = entry.key;
final v = entry.value;
return LineChartBarData(
spots: v.asMap().entries.map((data) {
return FlSpot(data.key, data.value);
}).toList(),
color: chartColors[idx],
barWidth: 5,
isCurved: true,
dotData: FlDotData(show: true),
);
}).toList();
}).toList();
</code>
<code>List sections = listData.asMap().entries.map((data) { final idx = data.key; final listval = data.value; listval.entries.map((entry) { final k = entry.key; final v = entry.value; return LineChartBarData( spots: v.asMap().entries.map((data) { return FlSpot(data.key, data.value); }).toList(), color: chartColors[idx], barWidth: 5, isCurved: true, dotData: FlDotData(show: true), ); }).toList(); }).toList(); </code>
List sections = listData.asMap().entries.map((data) {
      final idx = data.key;
      final listval = data.value;

      listval.entries.map((entry) {
        final k = entry.key;
        final v = entry.value;

        return LineChartBarData(
          spots: v.asMap().entries.map((data) {
            return FlSpot(data.key, data.value);
          }).toList(),
          color: chartColors[idx],
          barWidth: 5,
          isCurved: true,
          dotData: FlDotData(show: true),
        );
      }).toList();
    }).toList();

This time the error is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<LineChartBarData>'.
- 'List' is from 'dart:core'.
- 'LineChartBarData' is from 'package:fl_chart/src/chart/line_chart/line_chart_data.dart' ('.pub-cache/hosted/pub.dev/fl_chart-0.69.0/lib/src/chart/line_chart/line_chart_data.dart').
lineBarsData: sections,
^
</code>
<code>Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<LineChartBarData>'. - 'List' is from 'dart:core'. - 'LineChartBarData' is from 'package:fl_chart/src/chart/line_chart/line_chart_data.dart' ('.pub-cache/hosted/pub.dev/fl_chart-0.69.0/lib/src/chart/line_chart/line_chart_data.dart'). lineBarsData: sections, ^ </code>
Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<LineChartBarData>'.
 - 'List' is from 'dart:core'.
 - 'LineChartBarData' is from 'package:fl_chart/src/chart/line_chart/line_chart_data.dart' ('.pub-cache/hosted/pub.dev/fl_chart-0.69.0/lib/src/chart/line_chart/line_chart_data.dart').
                    lineBarsData: sections,
                                  ^

You have missed some important thing, when converting an existing List to Map, each item is associated to auto-incremented integer key so that your list will become like the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
1 :{
'Sales': [12.0, 14.5, 15.6, 12.1, 15.2, 17.6, 12.8, 24.0, 35.4, 12.9]
},
2 :{
'Cost': [9.0, 12.5, 11.6, 7.1, 10.2, 14.6, 8.8, 18.0, 31.4, 10.9]
},
}
</code>
<code>{ 1 :{ 'Sales': [12.0, 14.5, 15.6, 12.1, 15.2, 17.6, 12.8, 24.0, 35.4, 12.9] }, 2 :{ 'Cost': [9.0, 12.5, 11.6, 7.1, 10.2, 14.6, 8.8, 18.0, 31.4, 10.9] }, } </code>
{
  1 :{
      'Sales': [12.0, 14.5, 15.6, 12.1, 15.2, 17.6, 12.8, 24.0, 35.4, 12.9]
    },

  2 :{
      'Cost': [9.0, 12.5, 11.6, 7.1, 10.2, 14.6, 8.8, 18.0, 31.4, 10.9]
    },

}

You have treated the key correctly, but you forgot that the data associated with the key is itself an encapsulated Map (Nested Map), so, it has its own key and value.

so to access that list :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>listval = data.value['Sales'];
listval = data.value['Cost'];
</code>
<code>listval = data.value['Sales']; listval = data.value['Cost']; </code>
listval = data.value['Sales'];
listval = data.value['Cost'];

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật