PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] How to use double value in Text widget in flutter

 July 20, 2022     double, flutter, integer, text     No comments   

Issue

I got a response from API which is

{"id":1,"title":"Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops","price":109.95,"description":"Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday","category":"men's clothing","image":"https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg","rating":{"rate":3.9,"count":120}},

I'm display price into Text widget but it gives me this error

type 'double' is not a subtype of type 'int' please let me know how to display double value in text widget

ProductModel class:

class ProductModel {
  ProductModel(
      {required this.title,
      required this.id,
      required this.urlImage,
      required this.price});

  String title;
  int id;
  String urlImage;
  String price;

  factory ProductModel.fromJson(Map<String, dynamic> json) => ProductModel(
      id: json["id"],
      title: json["price"],
      urlImage: json["image"],
      price: json["price"]);

  Map<String, dynamic> toJson() =>
      {"id": id, "title": title, "image": urlImage, "price": price};
}


Solution

Try below code hope its help to you.

Your var declaration:

double price = 109.95; 

Your Widget:

Using String Interpolation:

    Text(
        'Price: $price',
      ),

OR Using toString():

  Text(
         price.toString() ,
      ),

Result Screen-> enter image description here



Answered By - Ravindra S. Patil
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing