【问题标题】:Type int is not a subtype of type Stringint 类型不是 String 类型的子类型
【发布时间】:2019-12-03 12:14:37
【问题描述】:

所以,在我使用 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 页面

无需我调用图像,它就可以正常工作。

【问题讨论】:

  • 能否请您在调用 ProductDetails 的位置添加代码
  • 我已将产品页面代码添加到原帖

标签: android flutter flutter-layout flutter-dependencies


【解决方案1】:

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

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

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

【讨论】:

  • 那么,我现在该怎么办?
  • 我已经检查了我的系统两天了,看看可能是什么问题。我将 prod_price 传递给 product_detail_picture。我通过将正确的变量 pro_picture 传递给它来解决它。谢谢@Blasanka
  • @isoftTech 很高兴来到这里!如果这个答案解决了你的问题,你总是可以让它成为正确的答案。以便其他人将其视为解决方案。
【解决方案2】:

尝试给出像 final String product_detail_picture 这样的类型,并确保 product_detail_pictureString 而不是 int

final String product_detail_picture;

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

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

【讨论】:

  • 是的,我是从调用图像的产品页面中做到的。我称它为对象数组。 { "name": "Women Dress", "picture": "images/products/dress2.jpeg", "old_price": 100, "price": 59, },
  • 正如我在回答中所说,您需要将 product_detail_picture 声明为 String 这不应该是动态的
猜你喜欢
  • 2020-12-14
  • 2019-04-24
  • 2021-03-29
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多