Flutter > Provider > Firebase / 错误:在构建结帐页面时抛出了以下范围错误



我试图为我的电子商务应用程序构建结账屏幕,我正试图开发用于测试和学习Flutter的应用程序,提供商作为状态管理,firbase作为后端。当建立签出屏幕时,该错误显示在日志中:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following RangeError was thrown building CheckoutPage(dirty, dependencies:
[_InheritedProviderScope<OrderNotifier>, _InheritedProviderScope<List<Address>>, _InheritedTheme,
_LocalizationsScope-[GlobalKey#f7872], _InheritedProviderScope<CartNotifier>,
_InheritedProviderScope<AuthProvider>]):
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was:
[38;5;248mCheckoutPage[39;49m
When the exception was thrown, this was the stack:
[38;5;244m#0      List.[]  (dart:core-patch/growable_array.dart:166:60)[39;49m
[38;5;248m#1      CheckoutPage.build[39;49m
[38;5;244m#2      StatelessElement.build[39;49m
[38;5;244m#3      ComponentElement.performRebuild[39;49m
[38;5;244m#4      Element.rebuild[39;49m
[38;5;244m#5      ComponentElement._firstBuild[39;49m
[38;5;244m#6      ComponentElement.mount[39;49m
...     Normal element mounting (24 frames)
[38;5;244m#30     Element.inflateWidget[39;49m
[38;5;244m#31     MultiChildRenderObjectElement.mount[39;49m
...     Normal element mounting (136 frames)
[38;5;244m#167    Element.inflateWidget[39;49m
[38;5;244m#168    Element.updateChild[39;49m
[38;5;244m#169    RenderObjectElement.updateChildren[39;49m
[38;5;244m#170    MultiChildRenderObjectElement.update[39;49m
[38;5;244m#171    Element.updateChild[39;49m
[38;5;244m#172    ComponentElement.performRebuild[39;49m
[38;5;244m#173    StatefulElement.performRebuild[39;49m
[38;5;244m#174    Element.rebuild[39;49m
[38;5;244m#175    StatefulElement.update[39;49m
[38;5;244m#176    Element.updateChild[39;49m
[38;5;244m#177    ComponentElement.performRebuild[39;49m
[38;5;244m#178    Element.rebuild[39;49m
[38;5;244m#179    ProxyElement.update[39;49m
[38;5;244m#180    _InheritedNotifierElement.update[39;49m
[38;5;244m#181    Element.updateChild[39;49m
[38;5;244m#182    SingleChildRenderObjectElement.update[39;49m
[38;5;244m#183    Element.updateChild[39;49m
[38;5;244m#184    ComponentElement.performRebuild[39;49m
[38;5;244m#185    StatefulElement.performRebuild[39;49m
[38;5;244m#186    Element.rebuild[39;49m
[38;5;244m#187    StatefulElement.update[39;49m
[38;5;244m#188    Element.updateChild[39;49m
[38;5;244m#189    SingleChildRenderObjectElement.update[39;49m
[38;5;244m#190    Element.updateChild[39;49m
[38;5;244m#191    SingleChildRenderObjectElement.update[39;49m
[38;5;244m#192    Element.updateChild[39;49m
[38;5;244m#193    ComponentElement.performRebuild[39;49m
[38;5;244m#194    Element.rebuild[39;49m
[38;5;244m#195    StatelessElement.update[39;49m
[38;5;244m#196    Element.updateChild[39;49m
[38;5;244m#197    ComponentElement.performRebuild[39;49m
[38;5;244m#198    Element.rebuild[39;49m
[38;5;244m#199    ProxyElement.update[39;49m
[38;5;244m#200    Element.updateChild[39;49m
[38;5;244m#201    ComponentElement.performRebuild[39;49m
[38;5;244m#202    StatefulElement.performRebuild[39;49m
[38;5;244m#203    Element.rebuild[39;49m
[38;5;244m#204    BuildOwner.buildScope[39;49m
[38;5;244m#205    WidgetsBinding.drawFrame[39;49m
[38;5;244m#206    RendererBinding._handlePersistentFrameCallback[39;49m
[38;5;244m#207    SchedulerBinding._invokeFrameCallback[39;49m
[38;5;244m#208    SchedulerBinding.handleDrawFrame[39;49m
[38;5;244m#209    SchedulerBinding._handleDrawFrame[39;49m
[38;5;244m#213    _invoke  (dart:ui/hooks.dart:253:10)[39;49m
[38;5;244m#214    _drawFrame  (dart:ui/hooks.dart:211:3)[39;49m
(elided 3 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════════════════════════
[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following RangeError was thrown building CheckoutPage(dirty, dependencies: [_InheritedProviderScope<OrderNotifier>, _InheritedProviderScope<List<Address>>, _InheritedTheme, _LocalizationsScope-[GlobalKey#f7872], _InheritedProviderScope<CartNotifier>, _InheritedProviderScope<AuthProvider>]):[39;49m
RangeError (index): Invalid value: Valid value range is empty: 0
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mCheckoutPage[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      List.[]  (dart:core-patch/growable_array.dart:166:60)[39;49m
[38;5;248m#1      CheckoutPage.build[39;49m
[38;5;244m#2      StatelessElement.build[39;49m
[38;5;244m#3      ComponentElement.performRebuild[39;49m
[38;5;244m#4      Element.rebuild[39;49m
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
I/flutter (10118): firebase service placeOrder applied

我的结账页面代码是:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:propro/src/models/cart_model.dart';
import 'package:propro/src/models/product_model.dart';
import 'package:propro/src/models/user/address_model.dart';
import 'package:propro/src/providers/auth_notifier.dart';
import 'package:propro/src/providers/cart_notifier.dart';
import 'package:propro/src/providers/order_notifier.dart';
import 'package:propro/src/screens/checkout/widgets/checkout_card.dart';
import 'package:propro/src/services/firebase_service.dart';
import 'package:propro/src/widgets/app_bar.dart';
import 'package:propro/src/widgets/primary_button.dart';
import 'package:provider/provider.dart';
class CheckoutPage extends StatelessWidget {
String productList(BuildContext context, List<Cart> cartItems) {
String _productList = '';
for (int i = 0; i < cartItems.length; i++) {
_productList += (cartItems[i].product.name + ', ');
}
return _productList;
}
@override
Widget build(BuildContext context) {
FirebaseUser user = context.watch<AuthProvider>().user;
List<Address> addressList = Provider.of<List<Address>>(context) ?? [];
// List<Product> allProductList = Provider.of<List<Product>>(context) ?? [];
List<Cart> cartItems = context.watch<CartNotifier>().cartItems ?? [];
List<Product> productItems =
context.watch<CartNotifier>().productItems ?? [];
Address address = context.watch<OrderNotifier>().address;
return Scaffold(
appBar: ZhAppBar(
title: 'Checkout',
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: ListView(
// crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
CheckoutCard(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Products',
style: Theme.of(context).textTheme.headline3,
),
Text(productList(context, cartItems)),
],
),
hasOption: false,
),
CheckoutCard(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Contact Information',
style: Theme.of(context).textTheme.headline3,
),
Text(
'This order will delivered to ${user.email}',
),
],
),
hasOption: false,
),
CheckoutCard(
title: addressList == []
? Container()
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Address',
style: Theme.of(context).textTheme.headline3,
),
Row(
children: [
Text(addressList[0].line1 + ', '),
Text(addressList[0].line2 + ', '),
Text(addressList[0].city),
],
),
Row(
children: [
Text(addressList[0].state + ', '),
Text(addressList[0].country + ', '),
Text(addressList[0].zip),
],
),
],
),
hasOption: true,
),
CheckoutCard(
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Payment',
style: Theme.of(context).textTheme.headline3,
),
Text(
'You can pay cash on delivery',
),
],
),
hasOption: false,
),
CheckoutCard(
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Delivery',
style: Theme.of(context).textTheme.headline3,
),
Text(
'Deliveryman',
),
],
),
hasOption: false,
),
CheckoutCard(
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Promo Code',
style: Theme.of(context).textTheme.headline3,
),
Text(
'You can use promotion code here',
),
],
),
hasOption: false,
),
CheckoutCard(
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Points',
style: Theme.of(context).textTheme.headline3,
),
Text(
'You can earn 999 point with placing this order',
),
],
),
hasOption: false,
),
CheckoutCard(
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Total',
style: Theme.of(context).textTheme.headline3,
),
Text(
context.watch<CartNotifier>().total.toString(),
style: Theme.of(context).textTheme.headline1,
),
],
),
hasOption: false,
),
SizedBox(
height: 48,
),
Center(
child: ZhPrimaryButton(
child: Text('Order'),
//TODO: Palce an order function
onPressed: () async {
await FirebaseService()
.placeOrder(user, address, productItems);
print('firebase service placeOrder applied');
},
),
)
],
),
),
);
}
}

请帮我解决这个问题,并祝你阅读我的问题:(

您不能像以前那样比较列表:

addressList == [] ?  foo : bar;

这不起作用,并且将永远返回";条";。

相反:

addressList == null || addressList.isEmpty ?  foo : bar;

相关内容

  • 没有找到相关文章

最新更新