方法 '*' 在 null 上调用。接收器:空 尝试呼叫:*(空)


class _BillDetailsWidgetState extends State<BillDetailsWidget> {
_BillDetailsWidgetState(
{this.qtyCount,
this.singleServicePrice,
this.itemsTotal,
this.totalPayablePrice});
int qtyCount = 10;
int singleServicePrice = 100;
int itemsTotal;
int totalPayablePrice = 0;
String cartPriceTotal() {
totalPayablePrice = qtyCount * singleServicePrice;
return totalPayablePrice.toString();
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 10.0),
color: Colors.white,
child: Container(
color: Colors.white,
padding:
EdgeInsets.only(top: 20.0, bottom: 20.0, left: 20.0, right: 20.0),
child: Column(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Bill Details:',
style: kTextStyle,
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Item(s) Total',
style: kOtherTextStyle,
),
Text(
'249',
style: kOtherTextStyle,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Safety & Conveyance Charges',
style: kOtherTextStyle,
),
Text(
'₹ 80',
style: kOtherTextStyle,
),
],
),
SizedBox(
height: 10,
),
Container(
height: 1,
color: Colors.grey,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
cartPriceTotal(),
style: kOtherTextStyle,
),
],
),
],
),
],
),
),
);
}
}

有人能帮上忙吗!

════════小部件库捕获到异常═══════════════════════════════════════════════════════生成BillDetailsWidget时引发了以下NoSuchMethodError(脏,状态:_BillDetailsWidgetState#4bb59(:方法"*"是在null上调用的。接收方:空尝试调用:*(空(

导致错误的相关小部件是:账单详细信息小工具file:///C:/Users/admin/AndroidStudioProjects/gloveson/lib/Screens/Bookings%20Page/pricing.dart:185:13当抛出异常时,这是堆栈:#0 Object.noSuchMethod(dart:core-patch/Object_patch.dart:51:5(#1 _BillDetailsWidgetState.cartPriceTotal(包裹:手套/屏幕/预订%20页面/价格。dart:532:34(#2 _BillDetailsWidgetState.build(包:gloveson/Screens/Bookings%20Page/pricing.dart:593:23(#3 StatefulElement.build(包:flutter/src/widgets/framework.dart:4792:27(#4 ComponentElement.performRebuild(包:flutter/src/widgets/framework.dart:4675:15(。。。════════════════════════════════════════════════════════════════════════════════════════════════════在1280ms内重新加载了589个库中的5个。

您必须在构造函数方法中设置默认值,而不是字段本身。否则,如果您没有在创建_BillDetailsWidgetState构造函数的位置提供值,则所有值都将设置为null。当然,将null值相乘会导致出现错误。

class BillDetailsWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => _BillDetailsWidgetState();
}

在您的情况下,实际上就好像您在调用State构造函数,如下所示:

class BillDetailsWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => _BillDetailsWidgetState(
qtyCount: null,
singleServicePrice: null,
itemsTotal: null,
totalPayablePrice: null,
);
}

因此,这解决了问题:

_BillDetailsWidgetState(
{this.qtyCount = 10,
this.singleServicePrice = 100,
this.itemsTotal,
this.totalPayablePrice = 0});

int qtyCount;
int singleServicePrice;
int itemsTotal;
int totalPayablePrice;
//... omitted for brevity

尽管您可能需要重新思考StatelessWidget的解决方案,例如:

class BillDetailsWidget extends StatelessWidget {
final int qtyCount;
final int singleServicePrice;
const BillDetailsWidget({
Key key,
@required this.qtyCount,
@required this.singleServicePrice,
})  : assert(qtyCount != null),
assert(singleServicePrice != null),
super(key: key);
String cartPriceTotal() {
return (qtyCount * singleServicePrice).toString();
}
@override
Widget build(BuildContext context) {
// ... omitted for brevity
}
}

相关内容

最新更新