0

我尝试在我的应用程序中使用以下代码在图表中显示动物的重量。我的目标是通过按钮控制可变时间(例如 1 个月、6 个月、1 年……)。

             LineChart(
                          LineChartData(
                            lineTouchData: LineTouchData(enabled: true),
                            gridData: FlGridData(
                              show: false,
                              drawHorizontalLine: false,
                              drawVerticalLine: false,
                              getDrawingVerticalLine: (value) {
                                return FlLine(
                                  color: Colors.black,
                                  strokeWidth: 1,
                                );
                              },
                              getDrawingHorizontalLine: (value) {
                                return FlLine(
                                  color: const Color(0xff37434d),
                                  strokeWidth: 1,
                                );
                              },
                            ),
                            lineBarsData: [
                              LineChartBarData(
                                spots: [
                                  FlSpot(0, data_snap()['Historie_Inhalt']['0']),
                                  FlSpot(1, data_snap()['Historie_Inhalt']['1']),
                                  FlSpot(2, data_snap()['Historie_Inhalt']['2']),
                                  FlSpot(3, data_snap()['Historie_Inhalt']['3']),
                                  FlSpot(4, data_snap()['Historie_Inhalt']['4']),
                                  FlSpot(6, data_snap()['Historie_Inhalt']['5'])],
                                isCurved: true,
                                barWidth: 1,
                                colors: [
                                  Colors.black87,
                                ],
                                belowBarData: BarAreaData(
                                  show: true,
                                ),
                                aboveBarData: BarAreaData(
                                    show: true,
                                    colors: [Colors.grey.withOpacity(0.3)]
                                ),
                                dotData: FlDotData(
                                  show: false,
                                  getDotPainter: (spot, percent, barData, index) =>
                                      FlDotCirclePainter(
                                          radius: 10,
                                          color: Colors.deepOrange.withOpacity(0.5)),
                                ),
                              ),
                            ],
                            titlesData: FlTitlesData(
                              bottomTitles: SideTitles(
                                  showTitles: false,
                                  reservedSize: 10,
                                  getTitles: (value) {
                                    switch (value.toInt()) {
                                      case 0:
                                        return '2019';
                                      case 1:
                                        return '2020';
                                      case 2:
                                        return '2021';
                                      case 3:
                                        return '2022';
                                      default:
                                        return '';
                                    }
                                  }),
                              leftTitles: SideTitles(showTitles: true, margin: 3,
                                  reservedSize: SizeConfig.safeBlockHorizontal * 11
                              ),
                              rightTitles: SideTitles(showTitles: false),
                              topTitles: SideTitles(showTitles: false),
                            ),
                          ),
                          swapAnimationDuration: Duration(milliseconds: 1500),
                        )

对于第一个 FlSpot,我直接收到以下错误:

type 'String' is not a subtype of type 'int' of 'index'

到目前为止,我只从 Firebase 中检索了六个固定值,并使用以下代码显示它们:

FlSpot(5, data_snap()['P_1W_W']),

“P_1W_W”在下面的屏幕截图中与“名称”处于同一级别。

数据库看起来像这样('History_Inhalt' 被定义为一个数组):

在此处输入图像描述

4

0 回答 0