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

参数类型“List”无法分配给参数类型“List

卢景澄
2023-03-14

我对这个flutter简单图表代码有问题。在我尝试运行代码时显示此错误。有人能帮我吗......

参数类型'List

这是代码示例:

//Bar chart example
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

class SimpleBarChart extends StatelessWidget {
  final List<charts.Series> seriesList;
  final bool animate;

  SimpleBarChart(this.seriesList, {this.animate = false});

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


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

  /// Create one series with sample hard coded data.
  static List<charts.Series<OrdinalSales, String>> _createSampleData() {
    final data = [
      new OrdinalSales('Today', 55),
      new OrdinalSales('Yesterday', 25),
      new OrdinalSales('2 days', 100),
      new OrdinalSales('24 June', 75),
      new OrdinalSales('23 June', 75),
      new OrdinalSales('23 June', 75),
      new OrdinalSales('22 June', 75),
    ];

    return [
      new charts.Series<OrdinalSales, String>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

/// Sample ordinal data type.
class OrdinalSales {
  final String year;
  final int sales;

  OrdinalSales(this.year, this.sales);
}
Launching lib\main.dart on Chrome in debug mode... Waiting for connection from debug service on Chrome... lib/widgets/barChart.dart:24:7: Error: The argument type 'List<Series<dynamic, dynamic>>' can't be assigned to the parameter type 'List<Series<dynamic, String>>'.
 - 'List' is from 'dart:core'.
 - 'Series' is from 'package:charts_common/src/data/series.dart' ('/C:/flutter/.pub-cache/hosted/pub.dartlang.org/charts_common-0.11.0/lib/src/data/series.dart').
      seriesList,
      ^ Failed to compile application.

共有1个答案

鲁鹏
2023-03-14

序列列表变量类型从以下位置更改为:

final List<charts.Series> seriesList;

到:

final List<charts.Series<dynamic, String>> seriesList;

这很好用。

 类似资料: