Home > Net >  TypeError (type 'int' is not a subtype of type 'double') flutter
TypeError (type 'int' is not a subtype of type 'double') flutter

Time:03-25

I am trying to do a http.get request to fetch a data i have already posted(working perfectly) to firebase (realtime storage) but whennever I call the method that gets the data, It throws an error of _TypeError (type 'int' is not a subtype of type 'double') Please note, I am using provider state management

Below is the method used to fecth my data

Future<void> getAndSetProducts() async {
    const url = 'https://shop-12901-default-rtdb.firebaseio.com/products.json';
    try {
      final response = await http.get(Uri.parse(url));
      var extractedResponse =
          json.decode(response.body) as Map<String, dynamic>;
      List<Product> loadedProducts = [];
      extractedResponse.forEach((prodId, product) {
        loadedProducts.add(
          Product(
            id: prodId,
            title: product['title'],
            price: product['price'], //-Sure the error is from here but not sure of how to resolve it-
            imageUrl: product['imageUrl'],
            description: product['description'],
            isFavorite: product['isFavorite'],
          ),
        );
      });
      _items = loadedProducts;
      notifyListeners();
    } catch (error) {
      throw error; //--------TypeError (type 'int' is not a subtype of type 'double')-------
    }
  }

Below is also the method where I call my method above to perform its task

@override
  void didChangeDependencies() {
    if (_isInit) {
      setState(() {
      isLoading = true;
    });
      try {
        Provider.of<Products>(context).getAndSetProducts().then((_) {
          setState(() {
            isLoading = false;
          });
        });
      } catch (error) {
        print(error);
      }
    }
    _isInit = false;
    super.didChangeDependencies();
  }

CodePudding user response:

Try to fetch the price as a num then parse it to double.

Future<void> getAndSetProducts() async {
    const url = 'https://shop-12901-default-rtdb.firebaseio.com/products.json';
    try {
      final response = await http.get(Uri.parse(url));
      var extractedResponse =
          json.decode(response.body) as Map<String, dynamic>;
      List<Product> loadedProducts = [];
      extractedResponse.forEach((prodId, product) {
        loadedProducts.add(
          Product(
            id: prodId,
            title: product['title'],
            price: (product['price'] as num).toDouble(), //Try this
            imageUrl: product['imageUrl'],
            description: product['description'],
            isFavorite: product['isFavorite'],
          ),
        );
      });
      _items = loadedProducts;
      notifyListeners();
    } catch (error) {
      throw error; //--------TypeError (type 'int' is not a subtype of type 'double')-------
    }
  }

CodePudding user response:

You can Use .toDouble() function after your code(example:-> product['price'].toDouble) OR you can use double.parse(value) function after your code(example:-> double.parse(product['price']))

  • Related