gpt4 book ai didi

flutter - 'List'类型不是 'List'类型的子类型

转载 作者:行者123 更新时间:2023-12-03 04:45:34 33 4
gpt4 key购买 nike

我是Flutter的初学者。我想创建将数据显示为list,piechart和barchart的TabBarView。当我开始编码时,一切都很好。为了查看它是否正常工作,我在选项卡上附加了不同的颜色。首先,我添加了工作正常的数据列表。但是,当我尝试添加PieChart时,在Title出现此错误。我该怎么办 ?

import 'package:flutter/material.dart';
import 'package:tezversion1/models/Car.dart';
import 'package:charts_flutter/flutter.dart' as charts;

class ComplainPage extends StatefulWidget {
YearList year;
ComplainPage(this.year);
@override
State<StatefulWidget> createState() => _ComplainPageState(year);
}

class _ComplainPageState extends State {
YearList year;
_ComplainPageState(this.year);

List<charts.Series<Complain, String>> _seriesPieData;

_generateData() {
var pieData = [];

for (var i = 0; i < year.errorList.length; i += 2) {
pieData.add(new Complain(year.errorList.keys.elementAt(i),
year.errorList.values.elementAt(i)));

_seriesPieData.add(
charts.Series(
data: pieData,
domainFn: (Complain complain, _) => complain.complain,
measureFn: (Complain complain, _) => complain.complainCount,
id: 'Pie Chart of Complain Counts',
labelAccessorFn: (Complain row, _) => '${row.complainCount}',
),
);
}
}

@override
void initState() {
super.initState();
_seriesPieData = List<charts.Series<Complain, String>>();
_generateData();
}

@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF151026),
title: Text('Complains of year ${year.year}'),
bottom: TabBar(
tabs: <Widget>[
Tab(
text: 'Data',
),
Tab(
text: 'Pie Chart',
),
Tab(
text: 'Bar Chart',
),
],
),
),
body: TabBarView(children: <Widget>[
Card(
color: Colors.white,
elevation: 2.0,
child: ListView.builder(
itemCount: year.errorList.length,
itemBuilder: (context, index) {
Map<String, double> errorList = year.errorList;
return Column(
children: <Widget>[
new ListTile(
title: Text(
capitalizer(errorList.keys.elementAt(index))),
subtitle: Text(decider(
errorList.keys.elementAt(index),
errorList.values.elementAt(index).toString())),
),
new Divider(
color: Colors.black,
height: 5,
thickness: 1,
indent: 5,
endIndent: 5,
),
],
);
})),
Container(
child: Center(
child: Column(
children: <Widget>[
Text(
'Pie Chart of Complain Counts for This Car',
style:
TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10.0,
),
Expanded(
child: charts.PieChart(_seriesPieData,
animate: true,
animationDuration: Duration(seconds: 5),
behaviors: [
new charts.DatumLegend(
outsideJustification:
charts.OutsideJustification.endDrawArea,
horizontalFirst: false,
desiredMaxRows: 2,
cellPadding:
new EdgeInsets.only(right: 4.0, bottom: 4.0),
entryTextStyle: charts.TextStyleSpec(
color: charts
.MaterialPalette.purple.shadeDefault,
fontFamily: 'Georgia',
fontSize: 11),
)
],
defaultRenderer: new charts.ArcRendererConfig(
arcWidth: 100,
arcRendererDecorators: [
new charts.ArcLabelDecorator(
labelPosition:
charts.ArcLabelPosition.inside)
]))),
],
),
),
),
Container(
color: Colors.purple,
)
]),
),
);
}

String capitalizer(String value) {
String capitalizedString = value[0].toUpperCase() + value.substring(1);
return capitalizedString;
}

String decider(String name, String value) {
if (name.startsWith("Average")) {
return "Average Milage : " + value + " miles";
} else {
return "Complain Count : " + value;
}
}
}

class Complain {
String complain;
double complainCount;

Complain(this.complain, this.complainCount);
}

最佳答案

我认为你需要改变

var pieData = [];


List<Complain> pieData = [];

因为您创建的是 _seriesPieData,它是char.Series的列表,其中 Complain的列表为数据,但是 pieData是动态的,如错误中所述。

如果它不起作用,请提供stacktrace以及触发此错误的行。

关于flutter - 'List<dynamic>'类型不是 'List<Complain>'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62392247/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com