Home > Back-end >  Why am i getting invalid double exception
Why am i getting invalid double exception

Time:06-27

I have searched for similar threads here on stack overflow.But I couldnt find a solution.Tried to debug my app and realized that my app crashes here

 var price = double.parse(prices[i].replaceAll(",", "."));

I have been working for hours on this problem.

   var content = parser.parse(doc);
var allBooks =
    content.querySelectorAll("div.facet__products div.prd-main-wrapper");
var prices = allBooks
    .map((e) => e.getElementsByClassName("prd-price")[0].text.trim())
    .toList();
 
  for (int i = 0; i < allBooks.length; i  ) {

      var price =double.parse(prices[i].replaceAll(",", "."));
      var name = names[i].toString();
      var author = authors[i].toString();
      var img = imgs[i].toString();
      BookModel model = BookModel(name, author, img, price, "D.R");
      controller.books.add(model);
      print(allBooks);
    }

CodePudding user response:

Change var price to double price

double price = double.parse(prices[i].toString().replaceAll(",", "")) ?? 0.0;

Edit

double? price = double.tryParse(prices[i].toString().replaceAll(",", ""));
  • Related