我在 Flutter 中使用 Firestore 和 Syncfusion Flutter Charts 时遇到问题。我正在从 firestore 查询一些文档,等待结果,所以函数必须是 Future,但我需要它是 a List
,而不是 a Future<List>
。这是我的代码:
@override
Widget build(BuildContext context) {
return Expanded(
child: SfCartesianChart(
primaryXAxis: CategoryAxis(
majorGridLines: const MajorGridLines(width: 0),
axisLine: const AxisLine(width: 0),
),
primaryYAxis: NumericAxis(
majorGridLines: const MajorGridLines(width: 0),
axisLine: const AxisLine(width: 0),
),
plotAreaBorderWidth: 0,
series: getData(),
),
);
}
String getcurrentMonth() {
DateTime date = DateTime.now();
return DateFormat.MMMM().format(date).substring(0, 3);
}
List<String> getOtherMonths() {
List<String> otherMonths = [];
DateTime date = DateTime.now();
for (var i = 1; i < 5; i++) {
DateTime prev = DateTime(date.year, date.month - i, date.day);
otherMonths.add(DateFormat.MMMM().format(prev).substring(0, 3));
}
return otherMonths;
}
Future<List<double>> getTotalByMonth() async {
List<double> totals = [];
DateTime date = DateTime.now();
for (var i = 0; i < 5; i++) {
var lastDay = DateTime(date.year, date.month - i + 1, 0);
DateTime startOfMonth = DateTime(date.year, date.month - i, 1);
DateTime endOfMonth =
DateTime(date.year, date.month - i, lastDay.day, 23, 59, 59, 999);
Timestamp startTimestamp = Timestamp.fromDate(startOfMonth);
Timestamp endTimestamp = Timestamp.fromDate(endOfMonth);
await FirebaseFirestore.instance
.collection('users/$uid/receipts')
.where('date', isGreaterThanOrEqualTo: startTimestamp)
.where('date', isLessThanOrEqualTo: endTimestamp)
.get()
.then((doc) {
double monthTot = 0;
doc.docs.forEach((element) {
monthTot += element.data()['total'];
});
totals.add(monthTot);
});
}
return totals;
}
Future<List<ChartSeries<BarChartModel, String>>> getData() async {
String currentMonth = getcurrentMonth();
List<String> months = getOtherMonths();
months.insert(0, currentMonth);
months = months.reversed.toList();
List<double> totals = await getTotalByMonth();
List<BarChartModel> data = [];
for (var i = 0; i < 5; i++) {
var obj = BarChartModel(months[i], totals[i],
i % 2 == 0 ? const Color(0xff003f9a) : const Color(0xff468fea));
data.add(obj);
}
return <ChartSeries<BarChartModel, String>>[
ColumnSeries<BarChartModel, String>(
dataSource: data,
xValueMapper: (BarChartModel data, _) => data.month,
yValueMapper: (BarChartModel data, _) => data.total,
pointColorMapper: (BarChartModel data, _) => data.month == currentMonth
? const Color(0xffeaa146)
: data.barColor,
animationDuration: 1000,
borderRadius: const BorderRadius.only(
topRight: Radius.circular(5),
topLeft: Radius.circular(5),
),
),
];
}
这是模型:
class BarChartModel {
final String month;
final double total;
final Color barColor;
BarChartModel(this.month, this.total, this.barColor);
}
这是我重新加载应用程序时遇到的错误:
type 'Future<List<ChartSeries<BarChartModel, String>>>' is not a subtype of type 'List<ChartSeries<dynamic, dynamic>>'
在这种情况下,摆脱 Future 类型的最佳方法是什么?