int 类型不是 String 类型的子类型
Type int is not a subtype of type String
因此,在我使用 Image.asset(widget.product_detail_picture) 调用图像之前,我的应用程序运行良好。
这是错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown building ProductDetails(dirty, state:
ProductDetailsState#db131):
type 'int' is not a subtype of type 'String'
Either the assertion indicates an error in the framework itself, or we should
provide substantially more information in this error message to help you
determine and fix the underlying cause.
这是我的产品详情页面代码:
import 'package:flutter/material.dart';
final product_detail_name;
final product_detail_picture;
final product_detail_old_price;
final product_detail_new_price;
ProductDetails({
this.product_detail_name,
this.product_detail_picture,
this.product_detail_old_price,
this.product_detail_new_price,
});
@override
_ProductDetailsState createState() => _ProductDetailsState();
}
class _ProductDetailsState extends State<ProductDetails> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
backgroundColor: Colors.red,
title: Text('HunkyBees'),
actions: <Widget>[
new IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
),
onPressed: () {}),
new IconButton(
icon: Icon(
Icons.shopping_cart,
color: Colors.white,
),
onPressed: () {})
],
),
body: new ListView(
children: <Widget>[
new Container(
height: 300.0,
child: GridTile(
child: Container(
color: Colors.white,
child: Image.asset(widget.product_detail_picture),
)),
),
],
),
);
}
}
这是我的产品页面,我从中调用产品图片
import '../pages/product_details.dart';
class Products extends StatefulWidget {
@override
_ProductsState createState() => _ProductsState();
}
class _ProductsState extends State<Products> {
var product_list = [
{
"name": "Men Real Dress",
"picture": "images/products/blazer1.jpeg",
"old_price": 120,
"price": 85,
},
{
"name": "Red Dress",
"picture": "images/products/dress1.jpeg",
"old_price": 190,
"price": 80,
},
{
"name": "Women Dress",
"picture": "images/products/dress2.jpeg",
"old_price": 100,
"price": 59,
},
{
"name": "Women Hills",
"picture": "images/products/hills1.jpeg",
"old_price": 140,
"price": 90,
},
];
@override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: product_list.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return Single_prod(
prod_name: product_list[index]['name'],
prod_picture: product_list[index]['picture'],
prod_old_price: product_list[index]['old_price'],
prod_price: product_list[index]['price'],
);
});
}
}
class Single_prod extends StatelessWidget {
final prod_name;
final prod_picture;
final prod_old_price;
final prod_price;
Single_prod(
{this.prod_name,
this.prod_picture,
this.prod_old_price,
this.prod_price});
@override
Widget build(BuildContext context) {
return Card(
child: Hero(
tag: prod_name,
child: Material(
child: InkWell(
onTap: () => Navigator.of(context).push(
new MaterialPageRoute(
//Passing Product Details Inside Navigation Co
builder: (context) => new ProductDetails(
product_detail_name: prod_name,
product_detail_picture: prod_old_price,
product_detail_new_price: prod_price,
product_detail_old_price: prod_old_price,
),
),
),
child: GridTile(
footer: Container(
color: Colors.white70,
child: ListTile(
leading: Text(
prod_name,
style: TextStyle(fontWeight: FontWeight.bold),
),
title: Text(
"$$prod_price",
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.w800),
),
subtitle: Text(
"$$prod_old_price",
style: TextStyle(
color: Colors.black54,
fontWeight: FontWeight.w800,
decoration: TextDecoration.lineThrough),
),
),
),
child: Image.asset(
prod_picture,
fit: BoxFit.cover,
)),
),
),
),
);
}
}
没有添加就可以正常工作:
child: Image.asset(widget.product_detail_picture),
到 product_details 页面
没有我调用图像它工作正常。
尝试提供类似于 final String product_detail_picture 的类型,并确保 product_detail_picture
是 String
而不是 int
。
final String product_detail_picture;
Image.asset 必须有字符串参数,例如
Image.asset('assets/images/cake.jpg'),
在你的 Single_prod
class 中,你对 product_detail_picture
的论证是 int
或 double
通过猜测你的解析参数变量名称(prod_old_price
):
ProductDetails(
product_detail_name: prod_name,
product_detail_picture: prod_old_price, // this prod_old_price is not String
最好使用静态类型,而不是使用动态类型。您缺少的语言的一大好处是您正面临这个问题,那个问题是 当您将 int
传递给 String
变量时,您没有收到编译时错误.
因此,在我使用 Image.asset(widget.product_detail_picture) 调用图像之前,我的应用程序运行良好。 这是错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown building ProductDetails(dirty, state:
ProductDetailsState#db131):
type 'int' is not a subtype of type 'String'
Either the assertion indicates an error in the framework itself, or we should
provide substantially more information in this error message to help you
determine and fix the underlying cause.
这是我的产品详情页面代码:
import 'package:flutter/material.dart';
final product_detail_name;
final product_detail_picture;
final product_detail_old_price;
final product_detail_new_price;
ProductDetails({
this.product_detail_name,
this.product_detail_picture,
this.product_detail_old_price,
this.product_detail_new_price,
});
@override
_ProductDetailsState createState() => _ProductDetailsState();
}
class _ProductDetailsState extends State<ProductDetails> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
backgroundColor: Colors.red,
title: Text('HunkyBees'),
actions: <Widget>[
new IconButton(
icon: Icon(
Icons.search,
color: Colors.white,
),
onPressed: () {}),
new IconButton(
icon: Icon(
Icons.shopping_cart,
color: Colors.white,
),
onPressed: () {})
],
),
body: new ListView(
children: <Widget>[
new Container(
height: 300.0,
child: GridTile(
child: Container(
color: Colors.white,
child: Image.asset(widget.product_detail_picture),
)),
),
],
),
);
}
}
这是我的产品页面,我从中调用产品图片
import '../pages/product_details.dart';
class Products extends StatefulWidget {
@override
_ProductsState createState() => _ProductsState();
}
class _ProductsState extends State<Products> {
var product_list = [
{
"name": "Men Real Dress",
"picture": "images/products/blazer1.jpeg",
"old_price": 120,
"price": 85,
},
{
"name": "Red Dress",
"picture": "images/products/dress1.jpeg",
"old_price": 190,
"price": 80,
},
{
"name": "Women Dress",
"picture": "images/products/dress2.jpeg",
"old_price": 100,
"price": 59,
},
{
"name": "Women Hills",
"picture": "images/products/hills1.jpeg",
"old_price": 140,
"price": 90,
},
];
@override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: product_list.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return Single_prod(
prod_name: product_list[index]['name'],
prod_picture: product_list[index]['picture'],
prod_old_price: product_list[index]['old_price'],
prod_price: product_list[index]['price'],
);
});
}
}
class Single_prod extends StatelessWidget {
final prod_name;
final prod_picture;
final prod_old_price;
final prod_price;
Single_prod(
{this.prod_name,
this.prod_picture,
this.prod_old_price,
this.prod_price});
@override
Widget build(BuildContext context) {
return Card(
child: Hero(
tag: prod_name,
child: Material(
child: InkWell(
onTap: () => Navigator.of(context).push(
new MaterialPageRoute(
//Passing Product Details Inside Navigation Co
builder: (context) => new ProductDetails(
product_detail_name: prod_name,
product_detail_picture: prod_old_price,
product_detail_new_price: prod_price,
product_detail_old_price: prod_old_price,
),
),
),
child: GridTile(
footer: Container(
color: Colors.white70,
child: ListTile(
leading: Text(
prod_name,
style: TextStyle(fontWeight: FontWeight.bold),
),
title: Text(
"$$prod_price",
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.w800),
),
subtitle: Text(
"$$prod_old_price",
style: TextStyle(
color: Colors.black54,
fontWeight: FontWeight.w800,
decoration: TextDecoration.lineThrough),
),
),
),
child: Image.asset(
prod_picture,
fit: BoxFit.cover,
)),
),
),
),
);
}
}
没有添加就可以正常工作:
child: Image.asset(widget.product_detail_picture),
到 product_details 页面
没有我调用图像它工作正常。
尝试提供类似于 final String product_detail_picture 的类型,并确保 product_detail_picture
是 String
而不是 int
。
final String product_detail_picture;
Image.asset 必须有字符串参数,例如
Image.asset('assets/images/cake.jpg'),
在你的 Single_prod
class 中,你对 product_detail_picture
的论证是 int
或 double
通过猜测你的解析参数变量名称(prod_old_price
):
ProductDetails(
product_detail_name: prod_name,
product_detail_picture: prod_old_price, // this prod_old_price is not String
最好使用静态类型,而不是使用动态类型。您缺少的语言的一大好处是您正面临这个问题,那个问题是 当您将 int
传递给 String
变量时,您没有收到编译时错误.