在Flutter应用中,我正在尝试实现在以下位置找到的OrdinalComboBarLineChart:https://github.com/google/charts。
它在调试模式下工作。但是,在发布模式下,它将引发错误,并且无法正确显示图表。我收到以下错误:
I/flutter (26577): The following RenderObject was being processed when the exception was fired:
I/flutter (26577): ChartContainerRenderObject<String>#8d79e NEEDS-PAINT
I/flutter (26577): parentData: <none>
I/flutter (26577): constraints: BoxConstraints(w=360.0, h=640.0)
I/flutter (26577): semantic boundary
I/flutter (26577): size: Size(0.0, 0.0)
I/flutter (26577): This RenderObject has no descendants.
我已经找到问题所在,这与MaterialApp小部件内的图表有关。就像我删除MaterialApp小部件时一样,它确实可以工作。
下面是产生此错误的代码:
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
void main() {
runApp(new OrdinalComboBarLineChart());
}
class OrdinalComboBarLineChart extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<charts.Series> seriesList = _createSampleData();
return MaterialApp(
home: Container(
height: 205.0,
width: 350.0,
child: charts.OrdinalComboChart(seriesList,
animate: false,
defaultRenderer: charts.LineRendererConfig(areaOpacity: 0.8, includeArea: true),
customSeriesRenderers: [
new charts.LineRendererConfig(customRendererId: 'LineOne'),
])));
}
/// Create series list with multiple series
List<charts.Series<OrdinalSales, String>> _createSampleData() {
var categoryOne = [
new OrdinalSales('0:00', 0),
new OrdinalSales('12:00', 0),
new OrdinalSales('23:59', 25),
];
var lineOne = [
new OrdinalSales('0:00', 30),
new OrdinalSales('12:00', 15),
new OrdinalSales('23:59', 7),
];
final Color myRed = const Color(0xFFEB4141);
final Color myDarkBackground = const Color(0xFF101926);
return [
new charts.Series<OrdinalSales, String>(
id: 'CategoryOne',
colorFn: (_, __) => charts.Color(
r: myRed.red, g: myRed.green, b: myRed.blue, a: myRed.alpha),
domainFn: (OrdinalSales sales, _) => sales.year,
measureFn: (OrdinalSales sales, _) => sales.sales,
data: categoryOne),
new charts.Series<OrdinalSales, String>(
id: 'LineOne ',
colorFn: (_, __) => charts.Color(
r: myDarkBackground.red,
g: myDarkBackground.green,
b: myDarkBackground.blue,
a: myDarkBackground.alpha),
domainFn: (OrdinalSales sales, _) => sales.year,
measureFn: (OrdinalSales sales, _) => sales.sales,
data: lineOne)
..setAttribute(charts.rendererIdKey, 'LineOne'),
];
}
}
/// Sample ordinal data type.
class OrdinalSales {
final String year;
final int sales;
OrdinalSales(this.year, this.sales);
}
我正在使用 chart_flutter:^ 0.5.0