Home > OS >  type 'double' is not a subtype of type 'int' when decoding data from json retu
type 'double' is not a subtype of type 'int' when decoding data from json retu

Time:02-13

Every time the data has to be decoded it returns null and the exception type 'double' is not a subtype of type 'int'. The problem is the method attached below.

Future<void> fetchProducts() async {
    final url = Uri.parse(
        'https://recipier-e1139-default-rtdb.europe-west1.firebasedatabase.app/recipes.json');
    final response = await http.get(url);
    final extractedData = json.decode(response.body) as Map<String, dynamic>;  
    print(extractedData['title']);
    if (extractedData.isEmpty || extractedData['error'] != null) {
      return;
    }
    List<Recipe> loadedRecipes = [];
    extractedData.forEach(
      (id, data) {
        loadedRecipes.add(
          Recipe(
            title: data['title'],
            description: data['description'],
            id: id,
            ingredients: data['ingredients'].cast<String>(),
            steps: data['steps'].cast<String>(),
            kcal: data['kcal'],
            p: data['p'],
            c: data['c'],
            f: data['f'],
            servings: data['servings'],
          ),
        );
      },
    );
    _recipes = loadedRecipes;
    notifyListeners();
  }

Here is my recipe model

class Recipe with ChangeNotifier {
  String title;
  String id;
  String description;
  File? image;
  bool favorite;
  List<String> ingredients;
  List<String> steps;
  int servings;
  int kcal;
  int p;
  int c;
  int f;

  Recipe({
    required this.title,
    required this.description,
    this.favorite = false,
    required this.id,
    this.image,
    required this.ingredients,
    required this.steps,
    required this.c,
    required this.kcal,
    required this.f,
    required this.p,
    required this.servings,
  });

I/flutter (31885): null E/flutter (31885): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'double' is not a subtype of type 'int' E/flutter (31885): #0
RecipeProvider.fetchProducts. package:recipier/providers/recipes_provider.dart:39 E/flutter (31885): #1 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8) E/flutter (31885): #2 RecipeProvider.fetchProducts package:recipier/providers/recipes_provider.dart:30 E/flutter (31885):

CodePudding user response:

One of the fields in your Recipe class is int but the json value for that corresponding field is returning a double.

You can solve it by finding the field and using .toInt()

Not sure which one of these is the int field but I'll use servings as an example.

            kcal: data['kcal'],
            p: data['p'],
            c: data['c'],
            f: data['f'],
            servings: data['servings'].toInt(),

CodePudding user response:

Use num type if you're not sure if the expected data is int or double. Also, if your app is null-safety aware and expected data can be null, use num? type.

CodePudding user response:

So I found a weird solution. Anyone else having this problem, I recommend you try this.

extractedData.forEach(
      (id, data) {
        loadedRecipes.add(
          Recipe(
            title: data['title'],
            description: data['description'],
            id: id,
            ingredients: data['ingredients'].cast<String>(),
            steps: data['steps'].cast<String>(),
            kcal: double.parse(data['kcal'].toString()).toInt(),
            p: double.parse(data['p'].toString()).toInt(),
            c: double.parse(data['c'].toString()).toInt(),
            f: double.parse(data['f'].toString()).toInt(),
            servings: double.parse(data['servings'].toString()).toInt(),
          ),
        );
      },
    );
  • Related