当我想使用 Getx 控制器获取数据并使用 grouped_list 将其显示为组列表时,因为我想让我的购物车被分组。但我得到以下错误,有人可以帮忙吗?谢谢。
下面的代码:
class CartList extends StatelessWidget {
final HomeController homeController = Get.find();
@override
Widget build(BuildContext context) {
var _elements = homeController.cartItems;
return Obx(() => GroupedListView<dynamic, String>(
elements: _elements,
groupBy: (element) => element['group'],
groupSeparatorBuilder: (String groupByValue) => Text(groupByValue),
itemBuilder: (context, dynamic element) => Text(element['name']),
itemComparator: (item1, item2) =>
item1['name'].compareTo(item2['name']), // optional
useStickyGroupSeparators: true, // optional
floatingHeader: true, // optional
order: GroupedListOrder.ASC, // optional
));
......
这里的错误:
The following assertion was thrown building GroupedListView<dynamic, String>(dirty, state: _GroupedListViewState<dynamic, String>#adcb0):
setState() or markNeedsBuild() called during build.
This Obx widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Obx
dirty
state: _ObxState#c2d8d
The widget which was currently being built when the offending call was made was: GroupedListView<dynamic, String>
dirty
state: _GroupedListViewState<dynamic, String>#adcb0