1

This is Product Models As Dummy Data

I can not show the price of product

import 'package:equatable/equatable.dart';

class ProductModels extends Equatable {
  final String name;
  final String category;
  final String imageUrl;
  final double price;
  final bool isRecommended;
  final bool isPopular;

  const ProductModels(
      {required this.name,
      required this.category,
      required this.imageUrl,
      required this.price,
      required this.isRecommended,
      required this.isPopular});

  @override
  // TODO: implement props
  List<Object?> get props =>
      [name, category, imageUrl, price, isRecommended, isPopular];
  static List<ProductModels> products = [
    const ProductModels(
        name: 'Pizza',
        category: 'Food',
        imageUrl: 'assets/product_images/photo-1565299624946-b28f40a0ae38.webp',
        price: 5.99,
        isRecommended: true,
        isPopular: false),
    const ProductModels(
        name: 'Pizza',
        category: 'Food',
        imageUrl: 'assets/pizza/foad-roshan-Y6OgisiGBjM-unsplash.jpg',
        price: 5.99,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Pizza',
        category: 'Food',
        imageUrl: 'assets/pizza/masimo-grabar-NzHRSLhc6Cs-unsplash.jpg',
        price: 5.99,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Pizza',
        category: 'Food',
        imageUrl: 'assets/pizza/saahil-khatkhate-kfDsMDyX1K0-unsplash.jpg',
        price: 5.99,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Pizza',
        category: 'Food',
        imageUrl: 'assets/pizza/saundarya-srinivasan-60nzTP7_hMQ-unsplash.jpg',
        price: 5.99,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Pizza',
        category: 'Food',
        imageUrl: 'assets/pizza/shaian-ramesht-exSEmuA7R7k-unsplash.jpg',
        price: 5.99,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Iphone Cover',
        category: 'Covers',
        imageUrl: 'assets/product_images/photo-1576107324820-c10884700b6b.webp',
        price: 19.50,
        isRecommended: false,
        isPopular: true),
    const ProductModels(
        name: 'Iphone Cover',
        category: 'Covers',
        imageUrl:
            'assets/iphone_covers/mark-chan-OPp_l7V2yCQ-unsplash.jpg',
        price: 19.50,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Iphone Cover',
        category: 'Covers',
        imageUrl:
            'assets/iphone_covers/daniel-korpai-qEoKzD2zJjE-unsplash.jpg',
        price: 19.50,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Iphone Cover',
        category: 'Covers',
        imageUrl: 'assets/iphone_covers/21013840.jpg',
        price: 19.50,
        isRecommended: false,
        isPopular: false),
    const ProductModels(
        name: 'Brown coat',
        category: 'Fashion',
        imageUrl: 'assets/product_images/photo-1583001809952-61891dfacb98.webp',
        price: 99.99,
        isRecommended: false,
        isPopular: true),
    const ProductModels(
        name: 'Iphone 11 Pro Max',
        category: 'Electronics',
        imageUrl: 'assets/product_images/photo-1592750475338-74b7b21085ab.webp',
        price: 800,
        isRecommended: true,
        isPopular: true),
    const ProductModels(
        name: 'Air Jordan AJI',
        category: 'Air Jordan Collection',
        imageUrl: 'assets/product_images/photo-1597045566677-8cf032ed6634.webp',
        price: 65,
        isRecommended: false,
        isPopular: true),
    const ProductModels(
        name: 'Orange Juice',
        category: 'Water',
        imageUrl: 'assets/product_images/photo-1600271886742-f049cd451bba.webp',
        price: 5,
        isRecommended: false,
        isPopular: true),
    const ProductModels(
        name: 'Air Pods Pro',
        category: 'Electronics',
        imageUrl: 'assets/product_images/photo-1606741965429-8d76ff50bb2f.webp',
        price: 200,
        isRecommended: true,
        isPopular: false),
    const ProductModels(
        name: 'Iphone 12',
        category: 'Electronics',
        imageUrl: 'assets/product_images/photo-1616348436168-de43ad0db179.webp',
        price: 700,
        isRecommended: true,
        isPopular: false),
  ];
}

and This is Wish List Models

import 'package:e_commerce_app/models/product_models.dart';
import 'package:equatable/equatable.dart';

import 'package:e_commerce_app/models/product_models.dart';
import 'package:equatable/equatable.dart';

class CartModels extends Equatable {
  final List<ProductModels>products;
  const CartModels({this.products=const <ProductModels>[]});

  double get subTotal =>
      products.fold(0, (total, current) => total + current.price);

  String get subTotlaToString => subTotal.toStringAsFixed(2);

  double deliveryFee(subTotal) {
    if (subTotal >= 30) {
      return 0.0;
    } else {
      return 10.0;
    }
  }

  String get deliveryFeeToString => deliveryFee(subTotal).toStringAsFixed(2);

  String freeDelivery(subTotal) {
    if (subTotal >= 30) {
      return 'You Have  Free Delivery';
    } else {
      double missing = 30.0 - subTotal;
      return 'Add\$ ${missing.toStringAsFixed(2)} For Free Delivery';
    }
  }

  String get freeDeliveryToString => freeDelivery(subTotal);
  double total(subTotal,deliveryFee){
    return subTotal+deliveryFee(subTotal);
  }
String get totalToString=>total(subTotal, deliveryFee).toStringAsFixed(2);
  @override
// TODO: implement props
  List<Object?> get props => [products];
}



i want to show the price of product and subtotal and total with cubit not bloc

this is cart Screen

this is my cubitStates


part of 'cart_cubit.dart';

abstract class CartState  {}

class CartInitial extends CartState {}

class CartLoading extends CartState {}

class CartLoaded extends CartState {
  final ProductModels productList;
   CartLoaded(this.productList,);
}
class CartError extends CartState {}

and this is cubit



import 'dart:developer';

import 'package:bloc/bloc.dart';
import 'package:e_commerce_app/models/cart_models.dart';
import 'package:e_commerce_app/models/product_models.dart';
import 'package:equatable/equatable.dart';

part 'cart_state.dart';

class CartCubit extends Cubit<CartState> {
  CartCubit() : super(CartInitial());

  List<ProductModels> productList = [];

  void getCartData(ProductModels productModels) async {
    emit(CartLoading());
     await Future<void>.delayed(const Duration(seconds: 1));
    try {
       productList.add(productModels) ;
      emit(CartLoaded((productModels)));
    } on Exception catch (e) {
      log(e.toString());
    }
  }
}

  1. I want to add product into cart screen with price details
  2. Add and remove items

1 Answers1

0

firstly, this information is still not enough to solve the problem. for example, it is not clear where and how the method is used getCartData.

then, looks like you have a little mess here. so to try fixed your problem, you should first get rid of mess.

fix name ProductModels to Product. why is it plural?.

same thing with CartModels.

then in state you will understand more correctly: you want to pass product model or list???

class CartLoaded extends CartState {
  final ProductModels productList;
   CartLoaded(this.productList,);
}

and then in cubit it will be more clear what exactly you want to emit in state: list(why not just CartModels if so??) or model Product:

...

    try {
           productList.add(productModels) ;
          emit(CartLoaded((productModels)));
        } 

...
Vladyslav Ulianytskyi
  • 1,401
  • 22
  • 22