当前位置: 首页 > 知识库问答 >
问题:

图表:参数类型“Future”不能分配给参数类型“List”

柴兴贤
2023-03-14

我试图制作一个简单的饼图,但在工厂构造函数中使用未来的数据时遇到了问题。错误信息显示:

预期有2个位置参数,但找到1个。

参数类型“未来”

以下是代码:

/// Simple pie chart example.
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';
import 'package:testingflutterapp/models/budget.dart';
import 'package:testingflutterapp/services/db.dart';

class SimplePieChart extends StatefulWidget {
  final List<charts.Series> seriesList;
  final bool animate;
  SimplePieChart(this.seriesList, this.animate);
  @override
  _SimplePieChartState createState() {
    return _SimplePieChartState(this.seriesList, this.animate);
  }
}

class _SimplePieChartState extends State<SimplePieChart> {
  List<charts.Series> seriesList;
  final bool animate;
  _SimplePieChartState(this.seriesList, this.animate);

  /// Creates a [PieChart] with sample data and no transition.
  factory _SimplePieChartState.withSampleData(int goal) {
    return _SimplePieChartState(
      _createSampleData(goal),
      animate: false,
      // Disable animations for image tests.
    );
  }

  @override
  Widget build(BuildContext context) {
    return new charts.PieChart(seriesList, animate: animate);
  }

  Future<Budget> giveData(int goal) async {
    List<Budget> _tasks = [];
    List<Map<String, dynamic>> _results =
        await DB.rawQuery('SELECT * FROM budget WHERE budget.id="$goal";');
    _tasks = _results.map((item) => Budget.fromMap(item)).toList();
    Budget budgetToDisplay = _tasks[0];
    return budgetToDisplay;
  }

  Future<double> giveDataSum(int goalID) async {
    List<Budget> _tasks = [];
    List<Map<String, dynamic>> _results = await DB.rawQuery(
        'SELECT sum(value) AS value FROM (SELECT budget_items.* FROM budget_items INNER JOIN budget_items_list ON budget_items_list.budget_items_id=budget_items.id INNER JOIN budget ON budget.id=budget_items_list.budget_id WHERE budget.id="$goalID");');
    _tasks = _results.map((item) => Budget.fromMap(item)).toList();
    double budgetToDisplay = _tasks[0].goal;
    return budgetToDisplay;
  }

  /// Create one series with sample hard coded data.
  Future<List<charts.Series<LinearSales, int>>> _createSampleData(
      int goal) async {
    // Calculate percentages
    //get goal value
    Budget budgetDisplay = await giveData(goal);
    double goalValue = budgetDisplay.goal;
    double keepValue = await giveDataSum(goal);
    double graphLine = goalValue - keepValue;
    final data = [
      new LinearSales(0, keepValue as int), // value saved = x
      new LinearSales(1, graphLine as int), // value to save = goal - x
    ];

    return [
      new charts.Series<LinearSales, int>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.year,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

/// Sample linear data type.
class LinearSales {
  final int year;
  final int sales;

  LinearSales(this.year, this.sales);
}

编辑:我能够使用Future Builder修复此问题。我的代码更改:

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:charts_flutter/flutter.dart';
import 'package:flutter/material.dart';
import 'package:testingflutterapp/models/budget.dart';
import 'package:testingflutterapp/services/db.dart';

// ignore: must_be_immutable
class SimplePieChart extends StatefulWidget {
  SimplePieChart(this.goal);

  int goal;
  @override
  _SimplePieChartState createState() {
    return _SimplePieChartState(this.goal);
  }
}

class _SimplePieChartState extends State<SimplePieChart> {

  List<charts.Series> seriesList;
  List<charts.Series> seriesListNew;
  bool animate;
  int goal;
  _SimplePieChartState(this.goal);

  /// Creates a [PieChart] with sample data and no transition.

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<charts.Series<LinearSales, int>>>(
        future: _createSampleData(goal),
        builder: (context, AsyncSnapshot<List<charts.Series<LinearSales, int>>> seriesList) {
          if (seriesList.hasData) {
            return charts.PieChart(
                seriesList.data,
                animate: false);
          } else {
            return CircularProgressIndicator();
          }
        }
    );
  }

  Future<Budget> giveData(int goal) async {
    List<Budget> _tasks = [];
    List<Map<String, dynamic>> _results =
        await DB.rawQuery('SELECT * FROM budget WHERE budget.id="$goal";');
    _tasks = _results.map((item) => Budget.fromMap(item)).toList();
    Budget budgetToDisplay = _tasks[0];
    return budgetToDisplay;
  }

  Future<double> giveDataSum(int goalID) async {
    List<Budget> _tasks = [];
    List<Map<String, dynamic>> _results = await DB.rawQuery(
        'SELECT sum(value) AS value FROM (SELECT budget_items.* FROM budget_items INNER JOIN budget_items_list ON budget_items_list.budget_items_id=budget_items.id INNER JOIN budget ON budget.id=budget_items_list.budget_id WHERE budget.id="$goalID");');
    _tasks = _results.map((item) => Budget.fromMap(item)).toList();
    double budgetToDisplay = _tasks[0].goal;
    String x = _results[0].values.toString().replaceAll(new RegExp(r'[\(,\)]'), "");
    double y = double.parse(x);
    return y;
  }

  /// Create one series with sample hard coded data.
  Future<List<charts.Series<LinearSales, int>>> _createSampleData(int goal) async {

    // Calculate percentages
    //get goal value
    Budget budgetDisplay = await giveData(goal);

    double goalValue = budgetDisplay.goal;

    double keepValue = await giveDataSum(goal);

    double graphLine = goalValue - keepValue;
    //print(graphLine.round());
    final data = [
      new LinearSales(0, keepValue.round()), // value saved = x
      new LinearSales(1, graphLine.round()), // value to save = goal - x
    ];


    return [
      new charts.Series<LinearSales, int>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.year,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: data,
      )
    ];
    //return seriesList;
  }
}

/// Sample linear data type.
class LinearSales {
  final int year;
  final int sales;

  LinearSales(this.year, this.sales);
}```

共有1个答案

陶健
2023-03-14

你的问题就在这里

return _SimplePieChartState(
  _createSampleData(goal),
  animate: false,
  // Disable animations for image tests.
);

>

\u SimplePieChartState有两个位置参数,您正在使用名为的动画参数。它应该是false而不是animate:false

 类似资料: