从 API 获取数据,但列表是空的,它在 30 分钟前工作

Fetch data from API, but the list is empty, it used to work 30 mins ago

从 api 获取应该是正确的,但它不起作用,因为我的“_movies”列表是空的。 2 小时前它是一个漂亮的应用程序,我不知道我为此做了什么更改。 它 returns 在 itemCount:_movies.length 时什么都没有或错误“无效值:有效值范围为空:0。我坚持这个。

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:task/models/movies.dart';
import 'package:http/http.dart' as http;

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<Movie> _movies = <Movie>[];
  var moviesUrl =
      'https://raw.githubusercontent.com/FEND16/movie-json-data/master/json/movies-coming-soon.json';
  Future<List<Movie>> getMovies() async {
    http.Response res = await http.get(Uri.parse(moviesUrl));
    try {
      if (res.statusCode == 200) {
        List<dynamic> movies = json.decode(res.body);
        return movies.map((e) => Movie.fromJson(e)).toList();
      } else {
        return <Movie>[];
      }
    } catch (e) {
      // print(e);
      return <Movie>[];
    }
  }

  @override
  void initState() {
    getMovies().then((value) {
      setState(() {
        _movies.addAll(value);
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Movies"),
      ),
      body: ListView.builder(
          itemCount: _movies.length,
          itemBuilder: (context, index) {
            return Card(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      _movies[index].title,
                      style:
                          TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
                    ),
                    Text(
                      _movies[index].genres.toString(),
                      style: TextStyle(
                        fontSize: 16,
                      ),
                    ),
                  ],
                ),
              ),
            );
          }),
    );
  }
}

似乎 _movies.lenght 是 0,但不知何故它在 2 小时前工作正常,我没有改变任何东西

class Movie {
  String id;
  String title;
  String year;
  List genres;
  // List ratings;
  String poster; //image
  // String contentRating;
  String duration;
  // DateTime releaseDate;
  // int averageRating;
  String originalTitle;
  String storyline;
  List actors;
  // String imdbRating;
  String posterurl; //image

  Movie({
    required this.id,
    required this.title,
    required this.year,
    required this.genres,
    // required this.ratings,
    required this.poster,
    // required this.contentRating,
    required this.duration,
    // required this.releaseDate,
    // required this.averageRating,
    required this.originalTitle,
    required this.storyline,
    required this.actors,
    // required this.imdbRating,
    required this.posterurl,
  });

  factory Movie.fromJson(Map<String, dynamic> json) {
    return Movie(
        id: json["id"],
        title: json["title"],
        year: json["year"],
        genres: json["genres"],
        // ratings: json["ratnigs"],
        poster: json["poster"],
        // contentRating: json["contentRating"],
        duration: json["duration"],
        // releaseDate: json["releaseDate"],
        // averageRating: json["averageRating"],
        originalTitle: json["originalTitle"],
        storyline: json["storyline"],
        actors: json["actors"],
        // imdbRating: json["imdbRating"],
        posterurl: json["posterurl"]);
  }
}

我相信错误一定在这一行:

getMovies().then((value) {
  setState(() {
    _movies.addAll(value);
  });
});

我认为 dart 对重建 _movies 变量感到困惑,因为您正在修改它而不是重新分配它。如果我是对的,解决方案就像重新分配 _movies:

一样简单
getMovies().then((value) {
  setState(() {
    _movies = [..._movies, ...value]
  });
});