dart 参数类型“Future〈List< PieChartSectionData>>”不能赋给参数类型“List< PieChartSectionData>?”

kfgdxczn  于 2023-02-20  发布在  Echarts
关注(0)|答案(2)|浏览(158)

我想显示从Firebase Firestore检索的图表,但它显示
参数类型'Future〈List〉'不能分配给行部分上的参数类型'List?':显示截面();
这是我的密码

children: <Widget>[
                                      const SizedBox(height: 18),
                                      Expanded(
                                        child: AspectRatio(
                                          aspectRatio: 1,
                                          child: PieChart(
                                            PieChartData(
                                              pieTouchData: PieTouchData(
                                                touchCallback:
                                                    (FlTouchEvent event,
                                                        pieTouchResponse) {},
                                              ),
                                              borderData: FlBorderData(
                                                  //show: false,
                                                  ),
                                              sectionsSpace: 0,
                                              centerSpaceRadius: 40,
                                              sections: showingSections(), // this is the error //
                                            ),
                                          ),
                                        ),
                                      ),
                                      Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.end,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: const <Widget>[
                                          Indicator(
                                            color: Colors.blue,
                                            text: 'Fiest',
                                            isSquare: true,
                                          ),
                                        ],
                                      ),
                                    ],

这是我的职责

Future<List<PieChartSectionData>> showingSections() async {
    int count = await subuhPrayedOnTime();
    int count2 = await subuhPrayedLate();
    double percentage1 = count * 0.4;
    double percentage2 = count2 * 0.10;

    return [
      PieChartSectionData(
        color: Colors.blueAccent,
        value: percentage1,
        title: '${percentage1.toStringAsFixed(0)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: Colors.black,
          //shadows: shadows,
        ),
      ),
      PieChartSectionData(
        color: Colors.blueAccent,
        value: percentage2,
        title: '${percentage2.toStringAsFixed(0)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: Colors.black,
          //shadows: shadows,
        ),
      ),
    ];

    
  }
jhkqcmku

jhkqcmku1#

尝试将函数的返回类型从

Future<List<PieChartSectionData>> showingSections() async {
    int count = await subuhPrayedOnTime();
    int count2 = await subuhPrayedLate();
    double percentage1 = count * 0.4;
    double percentage2 = count2 * 0.10;

    return [
      PieChartSectionData(
        color: Colors.blueAccent,
        value: percentage1,
        title: '${percentage1.toStringAsFixed(0)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: Colors.black,
          //shadows: shadows,
        ),
      ),
      PieChartSectionData(
        color: Colors.blueAccent,
        value: percentage2,
        title: '${percentage2.toStringAsFixed(0)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: Colors.black,
          //shadows: shadows,
        ),
      ),
    ];

    
  }

List<PieChartSectionData> showingSections() async {
    int count = await subuhPrayedOnTime();
    int count2 = await subuhPrayedLate();
    double percentage1 = count * 0.4;
    double percentage2 = count2 * 0.10;

    return [
      PieChartSectionData(
        color: Colors.blueAccent,
        value: percentage1,
        title: '${percentage1.toStringAsFixed(0)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: Colors.black,
          //shadows: shadows,
        ),
      ),
      PieChartSectionData(
        color: Colors.blueAccent,
        value: percentage2,
        title: '${percentage2.toStringAsFixed(0)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 20,
          fontWeight: FontWeight.bold,
          color: Colors.black,
          //shadows: shadows,
        ),
      ),
    ];

    
  }
30byixjq

30byixjq2#

showingSections是将来的方法,您需要等待/FutureBuilder提取数据。
对于您的情况,在状态类[if stateFullWidget]上创建一个future。

late final dFuture = showingSections();

并使用类似

Expanded(
  child: FutureBuilder<List<PieChartSectionData>>(
      future: dFuture,
      builder: (context, snapshot) {
        if (snapshot.hasData && snapshot.data != null) {
          return AspectRatio(
            aspectRatio: 1,
            child: PieChart(
              PieChartData(
                pieTouchData: PieTouchData(
                  touchCallback:
                      (FlTouchEvent event, pieTouchResponse) {},
                ),
                borderData: FlBorderData(
                    //show: false,
                    ),
                sectionsSpace: 0,
                centerSpaceRadius: 40,
                sections: snapshot.data!, // this is the error //
              ),
            ),
          );
        }
        return CircularProgressIndicator();
      }),
),

了解更多关于FutureBuilder的信息

相关问题