skip to Main Content

I want to display a chart that retrieve from firebase firestore but its says that

The argument type ‘Future<List<PieChartSectionData>>’ can’t be assigned to the parameter type ‘List<PieChartSectionData>?’ on the line sections: showingSections();

. Here are my codes,

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,
                                          ),
                                        ],
                                      ),
                                    ],

this is my function,

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,
        ),
      ),
    ];

    
  }

2

Answers


  1. Try changing return type of the function from

    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,
            ),
          ),
        ];
    
        
      }
    

    To

    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,
            ),
          ),
        ];
    
        
      }
    
    Login or Signup to reply.
  2. showingSections is a future method, you need to await/FutureBuilder to fetch data.

    For your case, create a future on state class[if stateFullWidget].

    late final dFuture = showingSections();
    

    and use like

    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();
          }),
    ),
    
    

    Find more about FutureBuilder

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search