How to convert double to string in Text Widget in Flutter

13,412

Double To String

productprice.toString().

String To Double

double.parse(productprice.toString())

Share:
13,412
mohammed nabil
Author by

mohammed nabil

Updated on June 09, 2022

Comments

  • mohammed nabil
    mohammed nabil almost 2 years

    I create two pages first page is home page which display the image and name, second page is the detail page which shows image, name and price. Now problem is if i click on the image it should be display the image,name and price in second page but it is showing an error of type 'double' is not a subtype of type string even i tried to convert it to string. Below is the code of two pages and one dart class model.

    HomePage.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),),),],)),),),),);}}
    

    Quantities.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
            ],),),);}}
    

    Product.dart

    class Product {
      String name;
      String image;
      double price;
      Product({this.name, this.price,this.image});}
    
  • Dominic
    Dominic over 2 years
    Or String toStringAsFixed(int fractionDigits) if you want to fix the precision