未处理的异常NoSuchMethodError:方法'[]'在flutter中被调用为null

时间:2020-09-08 06:37:21

标签: flutter dart

代码代表根据控制台指出的代码行有错误的代码行

错误显示为三个部分。 第一个:

new FlickrPhotoList.fromJson(软件包:flutter_app_new_flutter / flickerPhotoList.dart:22:26)


 import 'package:flutter_app_new_flutter/model.dart';

class FlickrPhotoList {
  int page;
  int pages;
  int perpage;
  int total;
  List<FlickrPhoto> photo;

  FlickrPhotoList({
    this.page,
    this.pages,
    this.perpage,
    this.total,
    this.photo
  });

  factory FlickrPhotoList.fromJson(Map<String, dynamic> parsedJson) {

    var list = parsedJson['photo'] as List;
    List<FlickrPhoto> photoList = list.map((i) => FlickrPhoto.fromJson(i)).toList();

    return FlickrPhotoList(
        page: parsedJson['page'],
        pages: parsedJson['pages'],
        perpage: parsedJson['perpage'],
        total: parsedJson['total'],
        photo: photoList);
  }
}

第二个错误显示在下面:

新的FlickrResult.fromJson(软件包:flutter_app_new_flutter / flickerResult.dart:15:37)


import 'package:flutter_app_new_flutter/flickerPhotoList.dart';

    class FlickrResult {
      FlickrPhotoList photos;
      String stat;
    
      FlickrResult({
        this.photos,
        this.stat
      });
    
      factory FlickrResult.fromJson(Map<dynamic, dynamic> parsedJson) {
    
        var photoList = FlickrPhotoList.fromJson(parsedJson['photos']);
    
        return FlickrResult(
            photos: photoList,
            stat: parsedJson['stat']
        );
      }
    }

这是第三个错误:

_SearchState.getSearchWallpapers。 (package:flutter_app_new_flutter / search.dart:47:33)


getSearchWallpapers(String query) async{

    String requestUrl = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b099758ce0d1e1bf8697f8491f7fb5fc&format=json&nojsoncallback=1&safe_search=1&text=$query";
    var file = await DefaultCacheManager().getSingleFile(requestUrl);
    http.Response response = await http.get(requestUrl);
    String lines = await file.readAsString();


    setState(() {
      Map userMap = jsonDecode(lines);
      flickrData = FlickrResult.fromJson(userMap);
      resultsCount = flickrData.photos.photo.length;
    });
  }

1 个答案:

答案 0 :(得分:0)

昨天发生在我身上的不是某些网站,我通过返回带有标题的响应来修复它。没有此类方法错误的错误是当您的模型与网站或服务器的API响应不匹配时,没有提供您的请求示例,这就是错误

return FlickrPhotoList(
        page: parsedJson['page'], 

检查parsedJson是否提供“页面”;因此,首先向邮递员或任何其他人提出请求,然后阅读您的API响应。

当我们改进 对于第三个,请同时发布search.dart