Flutter 类型“Future<List<dynamic>>”不是类型转换中“List<dynamic>”类型的子类型
Posted
技术标签:
【中文标题】Flutter 类型“Future<List<dynamic>>”不是类型转换中“List<dynamic>”类型的子类型【英文标题】:Flutter type 'Future<List<dynamic>>' is not a subtype of type 'List<dynamic>' in type cast 【发布时间】:2021-10-20 07:12:32 【问题描述】:我收到了这个错误:
类型“Future”不是类型转换中“List”类型的子类型
无法从未来投射列表
最终名单 caMois = List.from(BddAMKGraphCA().getDataGraphCA('2021') as List).toList();
我使用包mysql1而不是php json来绑定数据:有效,我可以看到结果
我该如何解决这个错误?
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'bdd_graph_annee.dart';
class SimpleBarChart extends StatelessWidget
final String anneeCA;
SimpleBarChart(Key? key, required this.anneeCA) : super(key: key);
final List<CaMoisData> caMois =
List<CaMoisData>.from(BddAMKGraphCA().getDataGraphCA('2021') as List).toList();
@override
Widget build(BuildContext context)
return Scaffold(
appBar: new AppBar(title: const Text('CA PAR MOIS')),
body: Center(
child: Container(
child: SfCartesianChart(
title: ChartTitle(text: 'CA DE 2021'),
//legend: Legend(isVisible: true),
series: <ChartSeries>[
BarSeries<CaMoisData, String>(
animationDuration: 1500,
dataSource: caMois,
xValueMapper: (CaMoisData monCa, _) => monCa.mois,
yValueMapper: (CaMoisData monCa, _) => monCa.ca,
dataLabelSettings: DataLabelSettings(isVisible: true)),
],
primaryXAxis: CategoryAxis(),
primaryYAxis: NumericAxis(
edgeLabelPlacement: EdgeLabelPlacement.shift,
//title: AxisTitle(text: 'CA DE 2021'),
)))));
import 'constant/bdd_constant.dart';
import 'package:mysql1/mysql1.dart';
// la classe de données
class CaMoisData
final String mois;
final double ca;
CaMoisData(this.mois, this.ca); //, this.y2);
class BddAMKGraphCA
Future<List<dynamic>> getDataGraphCA(String anneeCA) async
final conn = await MySqlConnection.connect(ConnectionSettings(
host: ConstantBdd.host,
port: ConstantBdd.port,
user: ConstantBdd.user,
password: ConstantBdd.password,
db: ConstantBdd.db,
));
var sql = "SELECT ";
sql += "'Janv',replace(replace(format(ifnull(sum(case when DATE_FORMAT(cmd_date,'%m/%Y')='01/" +anneeCA +"' then cmd_ttc else 0 end),'0'), 2), ',',' '),'.',',') as 'tot_01',";
sql += "'Févr',replace(replace(format(ifnull(sum(case when DATE_FORMAT(cmd_date,'%m/%Y')='02/" +anneeCA +"' then cmd_ttc else 0 end),'0'), 2), ',',' '),'.',',') as 'tot_02',";
sql += "'Mars',replace(replace(format(ifnull(sum(case when DATE_FORMAT(cmd_date,'%m/%Y')='03/" +anneeCA +"' then cmd_ttc else 0 end),'0'), 2), ',',' '),'.',',') as 'tot_03',";
sql += "'Avril',replace(replace(format(ifnull(sum(case when DATE_FORMAT(cmd_date,'%m/%Y')='04/" +anneeCA +"' then cmd_ttc else 0 end),'0'), 2), ',',' '),'.',',') as 'tot_04',";
sql += "'Mai',replace(replace(format(ifnull(sum(case when DATE_FORMAT(cmd_date,'%m/%Y')='05/" +anneeCA +"' then cmd_ttc else 0 end),'0'), 2), ',',' '),'.',',') as 'tot_05',";
sql += "FROM cmde ";
sql += "WHERE cmd_date between '" +anneeCA +"-01-01' and '" +anneeCA +"-12-31' order by cmd_date desc";
var results = await conn.query(sql);
await conn.close();
//print(results.toList());
return results.toList();
【问题讨论】:
【参考方案1】:那是因为getDataGraphCA()
返回了一个Future,意味着该值不会立即返回,你必须:
-
等待 (await keyword) 然后
setState()
更新用户界面,
在build
方法中使用FutureBuilder
【讨论】:
【参考方案2】:您需要在 Future 调用之前添加 await 关键字:
final List<CaMoisData> caMois =
List<CaMoisData>.from(BddAMKGraphCA().getDataGraphCA('2021') as List).toList();
改成
final List<CaMoisData> caMois = await
List<CaMoisData>.from(BddAMKGraphCA().getDataGraphCA('2021') as List).toList();
【讨论】:
【参考方案3】:我收到这条消息等待
【讨论】:
【参考方案4】:我找到了解决方案: 1- 创建一个 FutureBuilder 小部件来绑定来自 Mysql 的结果
2- 创建一个函数将数据绑定到列表
final List<CaMoisData> caMois = [];
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text("CA PAR MOIS"),
),
body: new Center(
child: FutureBuilder(
future: BddAMKGraphCA().getDataGraphCA('2021'),
builder: (context, AsyncSnapshot<List<dynamic>> snapshot)
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data!.length, // mettre le ! pour tester le null
itemBuilder: (context, int currentIndex) insertData(snapshot.data);
return affGraphCA(context);
);
else if (snapshot.hasError)
return Text('$snapshot.error');
//return a circular progress indicator.
return new CircularProgressIndicator();
)));
insertData(dataCA)
String moistemp; // = dataCA[0]['m01'].toString();
double catemp; //= double.parse(dataCA[0]['tot_01'].toString());
for (int i = 1; i < 13; i++)
moistemp = dataCA[0]['m' + i.toString().padLeft(2, '0')].toString();
catemp = double.parse(
dataCA[0]['tot_' + i.toString().padLeft(2, '0')].toString());
caMois.add(CaMoisData(moistemp, catemp));
Widget affGraphCA(BuildContext context)
return Container(
child: SfCartesianChart(
title: ChartTitle(text: 'CA DE 2021'),
//legend: Legend(isVisible: true),
series: <ChartSeries>[
BarSeries<CaMoisData, String>(
animationDuration: 1500,
dataSource: caMois,
xValueMapper: (CaMoisData monCa, _) => monCa.mois,
yValueMapper: (CaMoisData monCa, _) => monCa.ca,
dataLabelSettings: DataLabelSettings(isVisible: true)),
],
primaryXAxis: CategoryAxis(),
primaryYAxis: NumericAxis(
edgeLabelPlacement: EdgeLabelPlacement.shift,
//title: AxisTitle(text: 'CA DE 2021'),
)));
【讨论】:
以上是关于Flutter 类型“Future<List<dynamic>>”不是类型转换中“List<dynamic>”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter SQFLITE:请解释一下工厂和这个数据类型 Future<List<Trail>> 是啥意思
没有为类型“Future<List<Item>>”定义吸气剂“长度”
将 Future<List> 从 Firestore 转换为 List<String> - Flutter Firebase [重复]
如何在 Flutter 中获取 Future<List> 项的某个数据索引?