Home > Back-end >  Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. - Flutter
Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. - Flutter

Time:11-17

This is the error I get when request data from api - Performing hot restart... Syncing files to device sdk gphone x86 64 arm64... Restarted application in 777ms. E/flutter (21101): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. E/flutter (21101): Receiver: null E/flutter (21101): Tried calling: E/flutter (21101): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5) E/flutter (21101): #1 new Recipe.fromJson (package:food_recipe_app_1/models/recipe.dart:16:19) E/flutter (21101): #2 Recipe.recipesFromSnapshot. (package:food_recipe_app_1/models/recipe.dart:25:21) E/flutter (21101): #3 MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31) E/flutter (21101): #4 ListIterator.moveNext (dart:_internal/iterable.dart:342:26) E/flutter (21101): #5 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27) E/flutter (21101): #6 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28) E/flutter (21101): #7 new List.of (dart:core-patch/array_patch.dart:51:28) E/flutter (21101): #8 ListIterable.toList (dart:_internal/iterable.dart:213:44) E/flutter (21101): #9 Recipe.recipesFromSnapshot (package:food_recipe_app_1/models/recipe.dart:26:8) E/flutter (21101): #10 RecipeApi.getRecipe (package:food_recipe_app_1/models/recipe.api.dart:26:19) E/flutter (21101): E/flutter (21101): #11 _HomePageState.getRecipes (package:food_recipe_app_1/views/home.dart:25:16) E/flutter (21101): E/flutter (21101):

class Recipe {
  final String name;
  final String images;
  final double rating;
  final String totalTime;

  Recipe({
    this.name,
    this.images,
    this.rating,
    this.totalTime,
  });

  factory Recipe.fromJson(dynamic json) {
    return Recipe(
        name: json['name'] as String,
        images: json['images'][0]['hostedLargeUrl'] as String,
        rating: json['rating'] as double,
        totalTime: json['totalTime'] as String
    );
  }

  static List<Recipe> recipesFromSnapshot(List snapshot) {
    return snapshot.map((data) {
      return Recipe.fromJson(data);
    }).toList();
  }
  @override
  String toString() {
    return 'Recipe {name: $name, image: $images, rating: $rating, totalTime: $totalTime}';
  }
}

CodePudding user response:

Either json is null, or json['images'] returns null. There are other ways to do it, here is one option if you don't want to allow null values:

factory Recipe.fromJson(dynamic json) {
  if (json == null) return Recipe.empty();
  return Recipe(
      name: json['name'] ?? '',
      images: json['images']?[0]['hostedLargeUrl'] ?? '',
      rating: json['rating'] ?? 0,
      totalTime: json['totalTime'] ?? ''
  );
}

factory Recipe.empty() => Recipe(name: '', images: '', rating: '', totalTime: '');

CodePudding user response:

You haven't included enough code to be sure of where your error is exactly, but here is an example of what is happening so you can find it:

List<String>? _list;

right now _list is null.

If I call String _string = _list[0];

I will get the error you are experiencing. So your list is null somewhere.

  • Related