【问题标题】:Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. - Flutter未处理的异常:NoSuchMethodError:在 null 上调用了方法 \'[]\'。 - 颤振
【发布时间】:2022-11-17 05:18:35
【问题描述】:

这是我从 api 请求数据时得到的错误 - 执行热重启... 正在将文件同步到设备 sdk gphone x86 64 arm64 ... 在 777 毫秒内重新启动应用程序。 E/flutter (21101): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:NoSuchMethodError:在 null 上调用了方法 '[]'。 E/flutter (21101): 接收器: null E/flutter (21101):尝试调用: E/颤振 (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。 (包: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 新 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(包: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/颤动​​(21101): E/flutter (21101): #11 _HomePageState.getRecipes (包:food_recipe_app_1/views/home.dart:25:16) E/颤动​​(21101): E/颤动​​(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}';
  }
}

【问题讨论】:

标签: flutter api


【解决方案1】:

要么 json 为 null,要么 json['images'] 返回 null。还有其他方法可以做到这一点,如果您不想允许空值,这里有一个选项:

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: '');

【讨论】:

    【解决方案2】:

    您没有包含足够的代码来确定错误的确切位置,但这里有一个正在发生的事情的示例,因此您可以找到它:

    List<String>? _list;
    

    现在 _list 为空。

    如果我打电话给String _string = _list[0];

    我会得到你遇到的错误。所以你的列表在某处是空的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-08
      • 2020-04-25
      • 2021-08-06
      • 1970-01-01
      • 2020-02-23
      • 2020-01-11
      • 2021-02-11
      • 2021-06-10
      相关资源
      最近更新 更多