在 null 上调用 getter 'length' 导致错误屏幕

时间:2021-05-20 12:59:37

标签: flutter dart

我不知道 snapshot.data 怎么可能为空,因为我用一个元素初始化它以防止任何空列表(即使空列表不是空对象)。我认为问题出在 _searchChapter 函数中,因为在另一个屏幕上同样长度的工作。希望您能提供帮助并提前致谢。

class Chapters_detail extends StatelessWidget {
const Chapters_detail({Key key, @required this.manga,}) : super(key: key);

final Manga manga;


Future<List<MangaChapter>> _searchChapter (Manga manga) async {
  String manga_id = manga.id;
  var response = await http.get(Uri.parse('https://api.mangadex.org/manga/$manga_id/feed?limit=500&locales[]=en'));
  var data0 = jsonDecode(response.body);
  MangaChapter test = MangaChapter(id: '1', chapter: '123', hash: '12');
  List<MangaChapter> chapitres = [test];
  for (var result in data0['results']){
    MangaChapter temp = MangaChapter (
      id: result['data']['id'],
      chapter: result['data']['attributes']['chapter'],
      hash: result['data']['attributes']['hash'],
      data: result['data']['attributes']['data'],
      dataSaver: result['data']['attributes']['dataSaver'],
    );
    chapitres.add(temp);
  }
  return chapitres;

}


Widget build(BuildContext context) {
  return FutureBuilder(
    future: _searchChapter(manga),
    builder: (BuildContext context, AsyncSnapshot snapshot){

      return ListView.builder(
        itemCount: snapshot.data.length,
        itemBuilder: (BuildContext context, int index){
          return Column(
            children: List.generate(
                snapshot.data.length,
                    (index) => ListTile(
                  title: Text(snapshot.data[index].chapter),
                  leading: Icon(Icons.phone_android),
                  onTap: () {
                  },
                )
            ),
          );
        }
      );
    }
  );
}
}

0 个答案:

没有答案