知道为什么会发生这种情况吗?

mzsu5hc0  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(692)

我有一个可以使用hive的类,我想接收我正在使用的api的列表,但是当我调用得到列表的类时,它会给我这个错误

class PersistStorage {
  sendData(List<Movie> movie) async {
    var box = await Hive.openBox('data');
    box.put("movies", movie);
  }

}

futurebuilder,我称之为我的班级:

class GridViewMovies extends StatefulWidget {
  double _crossAxisSpacing = 15, _mainAxisSpacing = 12, _aspectRatio = 1;
  PersistStorage storage;

  GridViewMovies({Key key}) : super(key: key);

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

class _GridViewMoviesState extends State<GridViewMovies> {
  BlocMovies movieBloc;

  @override
  Widget build(BuildContext context) {
    movieBloc = BlocProvider.of(context);

    return FutureBuilder(
        future: movieBloc.getlistMovies(),
        builder: (BuildContext context, AsyncSnapshot<MoviesModel> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Center(
                child: CircularProgressIndicator(),
              );
              break;
            case ConnectionState.none:
              return Center(
                child: CircularProgressIndicator(),
              );
              break;

            case ConnectionState.done:
              if (snapshot.data == null) {
                return Center(child: Text("Intenta mas tarde"));
              }
              //here call my function .......................
              widget.storage.sendData(snapshot.data.items);

              return Center(
                child: movieList(context, snapshot.data.items),
              );
              break;

            case ConnectionState.active:
              if (snapshot.data == null) {
                return Center(child: Text("Intenta mas tarde"));
              }
              return Center(
                child: movieList(context, snapshot.data.items),
              );

              break;
            default:
          }
        });
  }

因为这个类只服务于调用我的函数,但却给了我那个错误,所以有什么办法解决这个问题吗?

hk8txs48

hk8txs481#

你是否在小部件上初始化了你的存储对象,我在你添加的代码中没有看到初始化。

class GridViewMovies extends StatefulWidget {
  PersistStorage storage;**// I MEAN THIS**
  GridViewMovies({Key key}) : super(key: key);
}

根据您的错误截图,senddata方法正在调用一个空对象storage.senddata(…)

相关问题