【问题标题】:How to fix this error Class 'List<items>' has no instance getter 'fruitsItemsList'. Receiver: Instance(length:1) of '_GrowableList'?如何修复此错误 类 'List<items>' 没有实例 getter 'fruitsItemsList'。接收方:“_GrowableList”的实例(长度:1)?
【发布时间】:2021-04-04 21:12:46
【问题描述】:

我正在尝试将商品添加到购物车并收到此错误:

“List”类没有实例获取器“fruitsItemsList”。 接收方:“_GrowableList”的实例(长度:1) 尝试调用:fruitsItemsList

这是我添加它们的代码行:

return StreamBuilder(// and it shows me erorr here two I do not know why? 
   //to add selected items to shopping cart
    stream: bloc.listStream,
    // ignore: missing_return
    builder: (context, snapshot) {
     // if loop to check snapshot not empty 
      if (snapshot.data != null) {
        return Row(
          children: <Widget>[
            Expanded(
              child: SizedBox(
                height: MediaQuery.of(context).size.height,
                child: ListView.builder(
                    shrinkWrap: true,
                    scrollDirection: Axis.vertical,
                    //to count the items that selected 
                    itemCount: snapshot.data.fruitsItemsList.fruitsItems.length,  // i think the erorr here 
                    itemBuilder: (context, int index) {
                      return Single_cart_product(
                        prodName:  snapshot.data.fruitsItemsList.fruitsItems[index].name,//to get item's name 
                        prodPrice: snapshot.data.fruitsItemsList.fruitsItems[index].price, //to get item's price
                        prodPic:   snapshot.data.fruitsItemsList.fruitsItems[index].picture, //to get item picture
                        prodQun:   snapshot.data.fruitsItemsList.fruitsItems[index].quantity, //to get item quantity
                      );
                    }),
              ),
            ),
          ],
        );
      }
    });

这是我的 items_List.dart:

FruitsItemsList fruitsItemsList = FruitsItemsList(fruitsItems: [
  item(
    id: 001,
    name: "kiwi",
    picture: "assets/kiwi1.jpg",
    price: 7,
  ),

]);

class FruitsItemsList {

  List<item> fruitsItems;
  FruitsItemsList({@required this.fruitsItems});
}

class item {
  int id;
  String name;
  double price;
  String picture;
  int quantity;

  item({
    @required this.id,
    @required this.name,
    @required this.price,
    @required this.picture,
    this.quantity = 1,
  });

}

【问题讨论】:

  • 请将类项目重命名为项目,以更准确地表示它是该对象中的单个项目。
  • 完成了,对不起
  • 而且不是单品,有5个。
  • 每个项目都是一个项目。这不是“物品”!您有一个 List,它看起来像 [item, item, item, item, item]。
  • 这样可以修复错误吗?

标签: android flutter dart


【解决方案1】:

没有类型转换的 Streambuilder 在返回快照时是标准的 List 类型。像这样投射:

StreamBuilder<List<Item>>

那么 snapShot.data 将是一个 Item 列表。当你调用 snapshot.data.fruitsItemsList.fruitsItems 时,它与 List.fruitsItemsList.fruitsItems 相同。 List 对象没有方法 fruitsItemsList。

【讨论】:

  • 不,先生,我已经解决了,问题出在这一行 itemCount: snapshot.data.fruitsItemsList.fruitsItems.length, 我刚刚做到了 snapshot.data.length 并且有效
  • 感谢您的帮助;)
猜你喜欢
  • 2021-03-20
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多