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

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

璩正志
2023-03-14

你好,我在小部件上的以下代码中出错

参数类型'List

这是我的代码

    import 'package:flutter/material.dart';
    // ignore: import_of_legacy_library_into_null_safe
    import 'package:charts_flutter/flutter.dart' as charts;
    
    class SimpleBarChart extends StatelessWidget {
      final List<charts.Series> seriesList;
      final bool? animate;
    
      SimpleBarChart(this.seriesList, {this.animate});
    
      factory SimpleBarChart.withSampleData() {
        return new SimpleBarChart(
          _createSampleData(),
          // Disable animations for image tests.
          animate: false,
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return new charts.BarChart(
          seriesList, //error this
          animate: animate,
        );
      }
    
      /// Create one series with sample hard coded data.
      static List<charts.Series<OrdinalSales, String>> _createSampleData() {
        final data = [
          new OrdinalSales('2014', 5),
          new OrdinalSales('2015', 25),
          new OrdinalSales('2016', 100),
          new OrdinalSales('2017', 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,
          )
        ];
      }
    }
    
    class OrdinalSales {
      final String year;
      final int sales;
    
      OrdinalSales(this.year, this.sales);
    }
}

我的代码与上面的图表插件不兼容吗?如何解决这个问题?

共有1个答案

袁英豪
2023-03-14

把你的班级从

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

对这个

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

 类似资料: