0
votes
    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,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Can anybody help with an error!

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ The following NoSuchMethodError was thrown building BillDetailsWidget(dirty, state: _BillDetailsWidgetState#4bb59): The method '*' was called on null. Receiver: null Tried calling: *(null)

The relevant error-causing widget was: BillDetailsWidget file:///C:/Users/admin/AndroidStudioProjects/gloveson/lib/Screens/Bookings%20Page/pricing.dart:185:13 When the exception was thrown, this was the stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) #1 _BillDetailsWidgetState.cartPriceTotal (package:gloveson/Screens/Bookings%20Page/pricing.dart:532:34) #2 _BillDetailsWidgetState.build (package:gloveson/Screens/Bookings%20Page/pricing.dart:593:23) #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4792:27) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:15) ... ════════════════════════════════════════════════════════════════════════════════════════════════════ Reloaded 5 of 589 libraries in 1,280ms.

1
totalPayablePrice = qtyCount * singleServicePrice; either qtyCount or singleServicePrice is null. Could you post the rest of your code where _BillDetailsWidgetState is called?Er1
Does this answer your question? What is a NoSuchMethod error and how do I fix it?nvoigt
@Er1 this is whole code! and as you can all the variables have values inside!Parichit Patel
Are you using statefull widjet? Why do you have constructer in private class?Yakhyo Mashrapov

1 Answers

0
votes

You have to set the default values in the constructor method instead of the fields themselves. Otherwise the values will all be set to null if you are not providing values where you are creating the _BillDetailsWidgetState constructor. Of course multiplying null values results in the error you get.

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

in your situation it's actually as if you were calling the State constructor like this:

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

So this fixes the issue:

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


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

Although you might want to rethink this solution to a StatelessWidget like so:

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
  }
}