我正在关注在线课程,但是我的代码抛出了一个错误,而教练的代码却没有。是的,我已对其进行扫描以确保它们相同。 也许我错过了一些小细节。无论如何,有人可以帮助我解决此错误,以便我继续吗? 这是get方法:
List<Map<String, Object>> get groupedTransactionValues {
return List.generate(7, (index) {
final weekDay = DateTime.now().subtract(
Duration(days: index),
);
var totalSum = 0.0;
for (int i; i < recentTransactions.length; i++) {
if (recentTransactions[i].date.day == weekDay.day &&
recentTransactions[i].date.month == weekDay.month &&
recentTransactions[i].date.year == weekDay.year) {
totalSum += recentTransactions[i].amount;
}
}
return {
'day': DateFormat.E().format(weekDay).substring(0, 1),
'amount': totalSum,
};
});
}
这是Flutter中的实现:
@override
Widget build(BuildContext context) {
return Card(
elevation: 6,
margin: EdgeInsets.all(20),
child: Row(
children: groupedTransactionValues.map((data) {
}).toList(),
),
);
}
}
这是错误消息:
The following NoSuchMethodError was thrown building Chart(dirty):
The method '<' was called on null.
Receiver: null
Tried calling: <(0)
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 Chart.groupedTransactionValues.<anonymous closure>
package:expense/widgets/chart.dart:19
#2 new List.generate (dart:core-patch/array_patch.dart:78:28)
#3 Chart.groupedTransactionValues
package:expense/widgets/chart.dart:13
#4 Chart.build
package:expense/widgets/chart.dart:45
#5 StatelessElement.build
package:flutter/…/widgets/framework.dart:4620
#6 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4546
#7 Element.rebuild
package:flutter/…/widgets/framework.dart:4262
#8 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4525
#9 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4520
#10 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#11 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5991
... Normal element mounting (109 frames)
#120 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#121 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5991
... Normal element mounting (210 frames)
#331 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#332 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:5991
... Normal element mounting (273 frames)
#605 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3490
#606 Element.updateChild
package:flutter/…/widgets/framework.dart:3258
#607 RenderObjectToWidgetElement._rebuild
package:flutter/…/widgets/binding.dart:1174
#608 RenderObjectToWidgetElement.mount
package:flutter/…/widgets/binding.dart:1145
#609 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>
package:flutter/…/widgets/binding.dart:1087
#610 BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2620
#611 RenderObjectToWidgetAdapter.attachToRenderTree
package:flutter/…/widgets/binding.dart:1086
#612 WidgetsBinding.attachRootWidget
package:flutter/…/widgets/binding.dart:927
#613 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>
package:flutter/…/widgets/binding.dart:908
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
答案 0 :(得分:2)
for (int i; i < recentTransactions.length; i++) {
这是导致错误的部分,因为已声明:“方法'<'在null上被调用。”您必须初始化int i = 0,因此代码应类似于
for (int i = 0; i < recentTransactions.length; i++) {