首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型int不是类型字符串的子类型。

类型int不是类型字符串的子类型。
EN

Stack Overflow用户
提问于 2019-12-03 12:14:37
回答 2查看 993关注 0票数 0

所以,我的应用程序工作得很好,直到我用Image.asset(widget.product_detail_picture)调用了一个图像。以下是错误:

代码语言:javascript
复制
══╡ 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.

以下是我的产品详细信息页面代码:

代码语言:javascript
复制
import 'package:flutter/material.dart';
代码语言:javascript
复制
  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),
            )),
          ),
        ],
      ),
    );
  }
}

这是我的产品页面,我在这里调用产品图像

代码语言:javascript
复制
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页面

不用我给图像打电话就行了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-03 13:02:55

Single_prod类中,通过猜测解析arg名称(prod_old_price),您对product_detail_picture的参数是intdouble

代码语言:javascript
复制
ProductDetails(
  product_detail_name: prod_name,
  product_detail_picture: prod_old_price, // this prod_old_price is not String

使用静态类型而不是动态类型是个好主意。您所缺少的语言有一个很大的好处,您正面临这个问题,这个问题是,当您将一个int String 传递给String变量时,您不会得到编译时错误。

票数 0
EN

Stack Overflow用户

发布于 2019-12-03 12:26:32

尝试提供一个类似于最终字符串product_detail_picture的类型,并确保product_detail_picture是一个String而不是int

代码语言:javascript
复制
final String product_detail_picture;

Image.asset必须具有字符串参数,例如,

代码语言:javascript
复制
 Image.asset('assets/images/cake.jpg'),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59156909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档