gpt4 book ai didi

string - 如何在 Flutter 中的 Text Widget 中将 double 转换为字符串

转载 作者:行者123 更新时间:2023-12-04 14:30:25 32 4
gpt4 key购买 nike

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

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

最佳答案

双转字符串 productprice.toString() .
字符串加倍double.parse(productprice.toString())

关于string - 如何在 Flutter 中的 Text Widget 中将 double 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61291465/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com