繁体   English   中英

如何在 Flutter 的文本小部件中将双精度转换为字符串

[英]How to convert double to string in Text Widget in Flutter

我创建了两个页面,第一页是显示图像和名称的主页,第二页是显示图像、名称和价格的详细信息页面。 现在的问题是,如果我单击图像,它应该在第二页显示图像、名称和价格,但它显示“double”类型的错误不是字符串类型的子类型,即使我试图将其转换为字符串。 下面是两页一页的代码 dart class model。

首页.dart

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();}
class _HomeState extends State<Home> {
List<Product> _product=[
  Product(
    name: "Small Cake",
    image: "assets/1.png",
    price: 50.00,
  ),];
  @override
  Widget build(BuildContext context) {
    return PlatformScaffold(
        body: ListView.builder(
            itemCount: _product.length,
            itemBuilder: (BuildContext context, int index) {
              return Products(
                product_image: _product[index].image,
                product_name: _product[index].name,
                product_price: _product[index].price,);}));}}
class Products extends StatelessWidget {
  final product_name;
  final product_image;
  final product_price;
  Products({this.product_name, this.product_image, this.product_price});
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(
          top: 35.0, bottom: 15.0, left: 20.0, right: 20.0),
      child: GestureDetector(
        onTap: (){
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => Quantities(
            productname: product_name,
            productprice: product_price,
            productimage: product_image,
          )));},
        child: Container(
          child: new FittedBox(
            child: Material(
                color: Colors.white,
                elevation: 15.0,
                borderRadius: BorderRadius.circular(15.0),
                shadowColor: Color(0x802196F3),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      width: 250,
                      height: 200,
                      child: ClipRRect(
                        borderRadius: new BorderRadius.circular(15.0),
                        child: new Image.asset(
                          product_image,
                          fit: BoxFit.cover,),),),
                    Padding(
                      padding: const EdgeInsets.only(top: 5.0,bottom: 5.0),
                      child: Text(product_name,style: TextStyle(color: Colors.blueGrey[700],
                          fontWeight: FontWeight.bold,fontSize: 18.0),),),],)),),),),);}}

数量.dart

class Quantities extends StatefulWidget {
  var productprice;
  String productimage;
  final productname;
  Quantities({this.productprice, this.productimage, this.productname});
  @override
  _QuantitiesState createState() => _QuantitiesState(productprice,productimage,productname);
}
class _QuantitiesState extends State<Quantities> {
  final productprice;
  final productimage;
  final productname;
  var finalprice;
  _QuantitiesState(this.productprice, this.productimage, this.productname);
@override
  void initState() {
  finalprice=double.parse(productprice);// tried to convert into string
    super.initState();}
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Details'),),
      body: Container(
      child: Column(
        children: <Widget>[
          Container(
            height: 200.0,
            child: GridTile(
              child: Container(
                color: Colors.white,
                child: Image.asset(productimage),),),),
          Text(productname),
          Text(finalprice.toString()),// This line getting an error of type double is not a subtype of string
        ],),),);}}

产品.dart

class Product {
  String name;
  String image;
  double price;
  Product({this.name, this.price,this.image});}

双转字符串

productprice.toString()

字符串加倍

double.parse(productprice.toString())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM