【问题标题】:The getter 'price' isn't defined for the type 'String'没有为“字符串”类型定义吸气剂“价格”
【发布时间】:2021-12-07 07:46:39
【问题描述】:

没有为“String”类型定义 getter 'id'。尝试导入定义 'id' 的库,将名称更正为现有 getter 的名称,或者定义一个名为 > 的 getter 或字段p>

当我使用购物车图标添加购物车商品时,我试图显示它们。当我点击购物车图标时,它会带我到 CartScrren 页面,它会显示我选择的项目。 但我无法从我的购物车中获取值。

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/cart.dart'  ;
import '../widgets/cart_items_display.dart';

class CartScreen extends StatelessWidget {
 
  static const route = '/cart';

  @override
  Widget build(BuildContext context) {
    final cart = Provider.of<Cart>(context);
    
    return Scaffold(
      appBar: AppBar(
        title: Text('Your Cart'),
      ),
      body: Column(
        children: <Widget>[
          Card(
            margin: EdgeInsets.all(25),
            child: Padding(
              padding: EdgeInsets.all(8),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    'Total',
                    style: TextStyle(fontSize: 25),
                  ),
                  Spacer(),
                  Chip(
                    label: Text(
                      '\$${cart.totalAmount}',
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                    backgroundColor: Theme.of(context).primaryColor,
                  ),
                  TextButton(onPressed: () {}, child: Text('Order Now')),
                ],
              ),
            ),
          ),
          SizedBox(
            height: 10,
          ),
          Expanded(
            child: ListView.builder(
              itemCount: cart.items.length,
              itemBuilder: (ctx, i) => CartItemDisplay(
              //Having problem Here**
               cart.items.values.toString()[i].id,
               cart.items.values.toString()[i].price,
               cart.items.values.toString()[i].quantity,
                cart.items.values.toString()[i].title,
                ///////**
              ),
            ),
          ),
        ],
      ),
    );
  }
}

下面是我的购物车,我正在使用这张地图。

import 'package:flutter/foundation.dart';

class CartItem {
  final String id;
  final String title;
  final int quantity;
  final double price;

  CartItem({
    required this.id,
    required this.title,
    required this.quantity,
    required this.price,
  });
}

class Cart with ChangeNotifier {
  Map<String, CartItem> _items ={} ;
  Map<String, CartItem> get items {
    return  {..._items};
  }
  int get itemCount {
    return _items.length;

  }

  double get totalAmount{
    var total=0.0;
    _items.forEach((key, cartItem) {
      total += cartItem.price*cartItem.quantity;
    });
    

    return total;
  }

  void addItem(String productId, double price, String title) {
    if (_items.containsKey(productId)) {
      _items.update(
          productId,
          (existingCartItem) => CartItem(
              id: existingCartItem.id,
              title: existingCartItem.title,
              quantity: existingCartItem.quantity + 1,
              price: existingCartItem.price));
    } else {
      _items.putIfAbsent(
          productId,
          () => CartItem(
                id: DateTime.now().toString(),
                title: title,
                quantity: 1,
                price: price,
              ));
    }
    notifyListeners();
  }
}

但是如果不使用 values.toString() 它会抛出这个错误

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    请尝试从购物车价值中获取购物车物品。

    final cart = Provider.of<Cart>(context);
    final cartItems = cart.items.entries.map((e) => e.value).toList();
    
    ...
    
         Expanded(
            child: ListView.builder(
              itemCount: cartItems.length,
              itemBuilder: (ctx, i) => CartItemDisplay(
              ////* Change these lines.
               cartItems[i].id,
               cartItems[i].price,
               cartItems[i].quantity,
               cartItems[i].title,
                ///////**
              ),
            ),
          ),
    ...
    

    【讨论】:

      【解决方案2】:

      你使用 getter 来获取属性:

      class CartItem {
        final String id;
        final String title;
        final int quantity;
        final double price;
      
        CartItem({
          required this.id,
          required this.title,
          required this.quantity,
          required this.price,
        });
        
        String get getId => id;
        String get getTitle => title;
        int get getQuantity => quantity;
        double get getPrice => price;
      }
      

      使用封装返回一个礼节总是好的。

      Expanded(
        child: ListView.builder(
          itemCount: cart.items.length,
          itemBuilder: (ctx, i) => CartItemDisplay(
            //Use getter
            cart.items.values.[i].getId,
            cart.items.values.[i].getPrice,
            cart.items.values.[i].getQuantity.toString(),
            cart.items.values.[i].getTitle.toString(),
          ),
        ),
      ),
      

      【讨论】:

      • 你犯了和 op 一样的错误。您应该调用对象获取器,然后调用 toString。 titleid 也不需要转换成字符串。
      • 先生,它显示的内容与之前相同''没有为类型'String'定义getter'getId'。尝试导入定义“getId”的库,将名称更正为现有 getter 的名称,或定义名为“getId”的 getter 或字段
      • @Md.FazleRabbiI 对不起,我忘了删除 toString ()。
      • @EliaTolin 确实如此。
      【解决方案3】:

      您可以使用此站点来构建模型类 https://javiercbk.github.io/json_to_dart/ 我认为这个网站对于创建你的模型类很有用 您可以将您的 json 放在这个网站上并获取模型类

      【讨论】:

        猜你喜欢
        • 2021-04-14
        • 2021-09-17
        • 2021-03-26
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 2021-08-28
        • 2021-10-04
        相关资源
        最近更新 更多