Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

How to change the String to Double in Flutter?

I change the String to Double? but I got this error. Please help me solve the problem. Thank you.

product.dart file (These was String before)

class Product {
  double ogPrice;
  double price;

productdetails.dart. Here's a file that has an error.

Row(
  mainAxisAlignment: MainAxisAlignment.start,
  mainAxisSize: MainAxisSize.max,
  crossAxisAlignment: CrossAxisAlignment.end,
  children: <Widget>[
    Text(
      '${Config().currency}${(widget.product.price)}',
      style: GoogleFonts.poppins(
        color: Colors.green.shade700,
        fontSize: 16.0,
        fontWeight: FontWeight.w600,
        letterSpacing: 0.3,
      ),
    ),
    SizedBox(
      width: 5.0,
    ),
    Text(
      '${Config().currency}${widget.product.ogPrice}',
      style: GoogleFonts.poppins(
        color: Colors.black54,
        decoration: TextDecoration.lineThrough,
        fontSize: 15.0,
        fontWeight: FontWeight.w500,
        letterSpacing: 0.3,
      ),
    ),
  ],
),

And I got this error

lib/widgets/product_detail.dart:43:43: Error: The argument type 'double' can't be assigned to the parameter type 'String'. (int.parse(widget.product.price) /

question from:https://stackoverflow.com/questions/66060983/how-to-change-the-string-to-double-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can write something like:

widget.product.ogPrice.toString()

or If you want to give digits after decimal point:

widget.product.ogPrice.toStringAsFixed(2)

Hope it will help. Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...