-2

I have a class apart to be able to use hive and I want to receive a list of an api that I'm consuming,but when I call my class who gets a list it gives me this error enter image description here

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

}

FutureBuilder where I call my class:

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

and because the class only serves me to call my function but throws me that mistake, any idea to solve this problem?

padaleiana
  • 955
  • 1
  • 14
  • 23
Steven Colocho
  • 381
  • 1
  • 5
  • 15

1 Answers1

0

did you initialize your storage object on the widget, I don't see the initialization anywhere in the code that you have added here.

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

as per your error screenshot that sendData method is calling on a null object which is storage.sendData(....)

Yakub Pasha
  • 483
  • 1
  • 5
  • 19