我试图制作一个简单的饼图,但在工厂构造函数中使用未来的数据时遇到了问题。错误信息显示:
预期有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); }```
你的问题就在这里
return _SimplePieChartState(
_createSampleData(goal),
animate: false,
// Disable animations for image tests.
);
>
\u SimplePieChartState
有两个位置参数,您正在使用名为的动画
参数。它应该是false
而不是animate:false
我对弗利特和我是新手;我一直在尝试开发一款带有PHP API后端的应用程序。在无数的答案和在线研究中,我无法找到解决我所面临问题的方法。我希望有人能告诉我我做错了什么。 我试图从API中获取数据,并将其显示在卡中。下面是我的代码: 希望能得到一些帮助。非常感谢。
我刚刚更新到Dart2和Flatter sdk:' 无法将参数类型“Object”指定给参数类型“ImageProvider”。), 我只是从弗利特开始,不知道该去哪里找别的。
由于最近的更新,我正面临这个问题,请伙计们帮帮我
我试图用flutter插件HTTP发出HTTP POST请求,但我得到了一个标题错误。有人知道这是什么原因吗?因为在我的其他应用程序中,这工作得非常好?
很好的一天。我不熟悉使用VSCode键入脚本。 获取以下错误: > 错误TS2322:类型'()= 代码: DTO。输电系统 LinkedObject.ts 我正在尝试使用上述接口方法实例化类: TravelClientFormPopulator.ts 任何帮助都将不胜感激。
当我将值从主页传递到源页面时,它显示了一个错误:参数类型“Future”无法分配给参数类型“uluFunction()”。(argument_type_not_assignable[strong text]lib\home.dart:15) 我哪里做错了?? 主页- 下面的页面是我想使用主页值的地方-