Fahmi Sawalha
Fahmi Sawalha

Reputation: 622

double and int problem in flutter using firebase values

hello iam new to flutter and iam facing this problem which i really don't know what to do about , i have this in my firestore : enter image description here

when i was getting my price values as int it works just fine and i can make a cart item , but when i changed them to double it get me the error that : double is not a subtype of int here is my :

Product Model:

import 'package:cloud_firestore/cloud_firestore.dart';

class ProductModel {
  static const ID = "id";
  static const NAME = "name";
  static const PICTURE = "picture";
  static const PRICE = "price";
  static const DESCRIPTION = "description";
  static const CATEGORY = "category";
  static const RECENT = "recent";
  static const QUANTITY = "quantity";
  static const BRAND = "brand";
  static const SALE = "sale";
  static const SIZES = "sizes";

  String _id;
  String _name;
  String _picture;
  String _description;
  String _category;
  String _brand;
  int _quantity;
  List _price;
  bool _sale;
  bool _recent;
  List _sizes;

  String get id => _id;

  String get name => _name;

  String get picture => _picture;

  String get brand => _brand;

  String get category => _category;

  String get description => _description;

  int get quantity => _quantity;

  List get price => _price;

  bool get featured => _recent;

  bool get sale => _sale;

  List get sizes => _sizes;

  ProductModel.fromSnapshot(DocumentSnapshot snapshot) {
    _id = snapshot.data()[ID];
    _brand = snapshot.data()[BRAND];
    _sale = snapshot.data()[SALE];
    _description = snapshot.data()[DESCRIPTION] ?? " ";
    _recent = snapshot.data()[RECENT];
    _price = snapshot.data()[PRICE];
    _category = snapshot.data()[CATEGORY];
    _sizes = snapshot.data()[SIZES];
    _name = snapshot.data()[NAME];
    _picture = snapshot.data()[PICTURE];

  }
}

CartModel:

class CartItemModel {
  static const ID = "id";
  static const NAME = "name";
  static const IMAGE = "image";
  static const PRODUCT_ID = "productId";
  static const PRICE = "price";
  static const SIZE = "size";
  static const QUANTITY = "quantity";

  String _id;
  String _name;
  String _image;
  String _productId;
  String _size;
  int _price;
  int _quantity;

  //  getters
  String get id => _id;

  String get name => _name;

  String get image => _image;

  String get productId => _productId;

  String get size => _size;

  int get price => _price;
  int get quantity => _quantity;



  CartItemModel.fromMap(Map data){
    _id = data[ID];
    _name =  data[NAME];
    _image =  data[IMAGE];
    _productId = data[PRODUCT_ID];
    _price = data[PRICE];
    _size = data[SIZE];
    _quantity = data[QUANTITY];
  }

  Map toMap() => {
    ID: _id,
    IMAGE: _image,
    NAME: _name,
    PRODUCT_ID: _productId,
    PRICE: _price,
    SIZE: _size,
    QUANTITY:_quantity,
  };
}

how i add to cart

  Future<bool> addToCart(
      {ProductModel product, String size, int index,int quantity}) async {
    try {
      var uuid = Uuid();
      String cartItemId = uuid.v4();
      List<CartItemModel> cart = _userModel.cart;

      Map cartItem = {
        "quantity":1,
        "id": cartItemId,
        "name": product.name,
        "image": product.picture,
        "productId": product.id,
        "price": product.price[index],
        "size": size,
      };

Upvotes: 1

Views: 1921

Answers (2)

Thierry
Thierry

Reputation: 8383

In Dart Programing Language, you can use integer literals to assign a value to a double variable:

double x = 1;

It will properly infer that you meant 1.0.

Note: This is tolerated only since Dart 2.1

However, Dart still is a Strongly Typed Programming Language. You cannot assign the value of an int variable to your double variable:

double x;
var y = 1; // Dart infer the type  y based on the value, hence an `int`.
x = y; // FAILS with "A value of type 'int' can't be assigned to a variable of type 'double'."

In your case, when you do _price = data[PRICE], the type of data[PRICE] will be inferred at runtime as int or double for, as an example, 10 or 9.99.

A solution would be to explicitly cast the value as _price = data[PRICE].toDouble();


More info about the Invalid Assignment error.

Upvotes: 5

Amon C
Amon C

Reputation: 1808

Change the variable type from int to double

class CartItemModel {
      static const ID = "id";
      static const NAME = "name";
      static const IMAGE = "image";
      static const PRODUCT_ID = "productId";
      static const PRICE = "price";
      static const SIZE = "size";
      static const QUANTITY = "quantity";
    
      String _id;
      String _name;
      String _image;
      String _productId;
      String _size;
      double _price;
      int _quantity;
    
      //  getters
      String get id => _id;
    
      String get name => _name;
    
      String get image => _image;
    
      String get productId => _productId;
    
      String get size => _size;
    
      double get price => _price;
      int get quantity => _quantity;
    
    
    
      CartItemModel.fromMap(Map data){
        _id = data[ID];
        _name =  data[NAME];
        _image =  data[IMAGE];
        _productId = data[PRODUCT_ID];
        _price = data[PRICE];
        _size = data[SIZE];
        _quantity = data[QUANTITY];
      }
    
      Map toMap() => {
        ID: _id,
        IMAGE: _image,
        NAME: _name,
        PRODUCT_ID: _productId,
        PRICE: _price,
        SIZE: _size,
        QUANTITY:_quantity,
      };
    }

Upvotes: 0

Related Questions