如何避免基于Charts_flutter DateTime的系列列表的运行时类型异常? [英] How can I avoid charts_flutter DateTime based Series List's runtime type exception?

查看:245
本文介绍了如何避免基于Charts_flutter DateTime的系列列表的运行时类型异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型:

class Record {
  final DateTime timeStamp;
  final int metric;
}

我想在最简单的上显示一系列内容线形图

List<Record> _sampledRecords;
...
@override
initState() {
  _sampledRecords = ...
}
...
              child: LineChart(
                [
                  Series<Record, DateTime>(
                    id: 'Metric 1',
                    colorFn: (_, __) =>
                        MaterialPalette.blue.shadeDefault,
                    domainFn: (Record record, _) => record.timeStamp,
                    measureFn: (Record record, _) => record.metric,
                    data: _sampledRecords,
                  ),
                ],
              ),

现在,这与几个示例代码完全一致我看到了,就像检出 https://google.github.io/charts/ flutter / example / axes / nonzero_bound_measure_axis
显然人数是那里的测量值(y轴数据),而 DateTime 是x轴数据。

Now this is completely in concert with several example code I saw, like check out https://google.github.io/charts/flutter/example/axes/nonzero_bound_measure_axis Clearly the headcount is the measurement there (y axis data) and the DateTime is the x axis data. I typed my use-case exactly the same way and the compiler is OK with it.

但是我得到的运行时是: type'List< Series< Record,DateTime> >不是类型为'List< Series< dynamic,num>>'的子类型。系统从何处提取此 num 数字键入?我无法使用接收 List<类型的运行时错误; series< dynamic,dynamic>>'不是'List< Series< dynamic,num>>'类型的子类型Charts_flutter (因此请意识到我的问题不是重复的)解决方案,因为 DateTime 不是 num 类型。我尝试对此进行澄清,但并没有做任何更改:

However runtime I get: type 'List<Series<Record, DateTime>>' is not a subtype of type 'List<Series<dynamic, num>>'. Where does the system pulls this num numeric typing from? I cannot use Receiving runtime error with type 'List<Series<dynamic, dynamic>>' is not a subtype of type 'List<Series<dynamic, num>>' charts_flutter (so please realize my question is NOT a duplicate) solution, because DateTime is not a num type. I tried this as a clarification, but it didn't change anything:

              child: LineChart(
                <Series<Record, DateTime>>[
                  Series<Record, DateTime>(
                    id: 'Metric 1',
                    colorFn: (_, __) =>
                        MaterialPalette.blue.shadeDefault,
                    domainFn: (Record record, _) => record.timeStamp,
                    measureFn: (Record record, _) => record.metric,
                    data: _sampledRecords,
                  ),
                ],
              ),




我重构了像这样的代码:


I refactored the code like this:

List<Series<Record, DateTime>> _getPowerData() {
  return [
    Series<Record, DateTime>(
      id: 'Metric 1',
      colorFn: (_, __) =>
      MaterialPalette.blue.shadeDefault,
      domainFn: (Record record, _) => record.timeStamp,
      measureFn: (Record record, _) => record.power,
      data: _sampledRecords,
    ),
  ];
}

              child: LineChart(_getPowerData()

我得到 type'List< Series< dynamic,dynamic>>'不是'List< Series< dynamic,num>>'运行时异常类型的子类型。使用 int 满足 num 类型:

I get type 'List<Series<dynamic, dynamic>>' is not a subtype of type 'List<Series<dynamic, num>>' runtime exception. Furthermore, I even tried to satisfy the num type with int:

List<Series<Record, int>> _getPowerData() {
  return [
    Series<Record, int>(
      id: 'Metric 1',
      colorFn: (_, __) =>
      MaterialPalette.blue.shadeDefault,
      domainFn: (Record record, _) => record.seconds,
      measureFn: (Record record, _) => record.power,
      data: _sampledRecords,
    ),
  ];
}

但即使这样也会产生类型的'List< Series< dynamic,dynamic>>'不是类型'List< Series< dynamic,num>>'的子类型/ code>。

But even this yields the type 'List<Series<dynamic, dynamic>>' is not a subtype of type 'List<Series<dynamic, num>>'.

推荐答案

问题是简单的 LineChart 不适合处理时间序列数据。让我们以 NonzeroBoundMeasureAxis 例如https://google.github.io/charts/flutter/example/axes/nonzero_bound_measure_axis ):它使用 TimeSeriesChart 。因此,我当时正在与 LineChart 进行角力比赛,而我本应该选择一个更专业的图表(有多个图表)来支持基于时间序列的数据。

The issue was that the simple LineChart is not geared to handle time series data. Let's take the NonzeroBoundMeasureAxis (https://google.github.io/charts/flutter/example/axes/nonzero_bound_measure_axis) for example: it uses TimeSeriesChart. So I was in a wrestling match with LineChart whereas I should have just picked a more specialized chart (there are multiple) which support time series based data.

这篇关于如何避免基于Charts_flutter DateTime的系列列表的运行时类型异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆