我试图从一个库中快速地运行图表。我无法使用main.dart。
Main.dart:
import './widgets/chart.dart';
void main() {
runApp(
new MaterialApp(
home: PointsLineChart(),
),
);
}
chart.dart:
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
class PointsLineChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;
PointsLineChart(this.seriesList, {this.animate});
/// Creates a [LineChart] with sample data and no transition.
factory PointsLineChart.withSampleData() {
return new PointsLineChart(
_createSampleData(),
// Disable animations for image tests.
animate: false,
);
}
@override
Widget build(BuildContext context) {
return new charts.LineChart(seriesList,
animate: animate,
defaultRenderer: new charts.LineRendererConfig(includePoints: true));
}
/// Create one series with sample hard coded data.
static List<charts.Series<LinearSales, int>> _createSampleData() {
final data = [
new LinearSales(0, 5),
new LinearSales(1, 25),
new LinearSales(2, 100),
new LinearSales(3, 75),
];
return [
new charts.Series<LinearSales, int>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
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);
}
我得到的错误是在main.dart中,我需要输入一些内容才能运行chart.dart。在PointsLineChart()中,
它给我错误:
预计需要1个自变量,但找到0个。dart(not_enough_required_arguments) (新)PointsLineChart(列表> seriesList,{布尔动画)}→PointsLineChart
答案 0 :(得分:1)
PointsLineChart的构造函数带有两个参数:
PointsLineChart(this.seriesList, {this.animate});
在main.dart中,您不提供任何参数:
home: PointsLineChart(),// here you need to add two arguements!!!
更改代码如下:
runApp(
new MaterialApp(
home: PointsLineChart(PointsLineChart.createSampleData(), animate: false),
),
);
还将_createSampleData更改为createSampleData以使其公开。