int类型不是String类型的子类型

时间:2019-12-03 12:14:37

标签: android flutter flutter-layout flutter-dependencies

因此,在我使用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页面

不用我调用图像就可以正常工作。

2 个答案:

答案 0 :(得分:0)

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

final String product_detail_picture;

Image.asset必须具有String参数,例如

 Image.asset('assets/images/cake.jpg'),

答案 1 :(得分:0)

在您的Single_prod类中,通过猜测解析arg可变名称(product_detail_picture),您对int的论点是doubleprod_old_price

ProductDetails(
  product_detail_name: prod_name,
  product_detail_picture: prod_old_price, // this prod_old_price is not String

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