0

I'm trying to access the moviesList inside the build() and results as follows. What is the real issue?

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building MyListScreen(dirty, state:
flutter: _MyListScreenState#21007):
flutter: The getter 'results' was called on null.
flutter: Receiver: null
flutter: Tried calling: results

The code snippet followed as below:

class _MyListScreenState extends State<MyListScreen> {

  MoviesList moviesList;

  _getLatestMovies() {
    APIService.getMoviesNowPlaying().then((response) {
      setState(() {
        final jsonData = json.decode(response.body);
        moviesList = new MoviesList.fromJson(jsonData);
        // moviesList(new MoviesList.fromJson(jsonData));

        for (var i = 0; i < moviesList.results.length; i++) {
          print(moviesList.results[i].title);
        }
      });
    });
  }

  @override
  initState() {
    super.initState();
    _getLatestMovies();
  }

  @override
  Widget build(context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("View Title"),
      ),
      body: ListView.separated(
        padding: EdgeInsets.zero,
        itemCount: moviesList.results.length,
        itemBuilder: (context, index) {
          // return ListTile(title: Text(moviesList.results[index].title));
        },
        separatorBuilder: (context, index) {
          return Divider();
        },
      ));
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Faisal
  • 1,332
  • 2
  • 13
  • 29

2 Answers2

0

Use

itemCount: moviesList != null ? moviesList.results.length : 0,

Your build() was getting called before moviesList was initialised, so here we first check for null, if null use 0 as itemCount else use its length.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • Thanks, it doesn't solve my issue. I need the moviesList.results data inside the build(). Any suggestion? – Faisal Jun 27 '19 at 16:43
  • The solution I provided is what you would be looking for, can you tell me in which line you are getting the error? – CopsOnRoad Jun 27 '19 at 16:45
  • Found the issue was due to the data flow. The `APIService.getMoviesNowPlaying()` was async, removing async results the proper data at itemCount with `moviesList.results.length`. – Faisal Jun 27 '19 at 17:14
  • Please explain HOW your code snippet solves the OP's issue. – ifconfig Jun 28 '19 at 01:12
  • @ifconfig OP is having issues because `build()` was getting called before `moviesList` was initialized, so here we check for `null`, if `null` use `0` as `itemCount` and if not null use `length`. – CopsOnRoad Jun 28 '19 at 07:03
  • Explain... in the answer. – ifconfig Jun 28 '19 at 13:48
0

Solution (probably one):

The real reason behind the issue wasn't variable scope, rather it was the way async did was completely wrong, a redesign solved the issue. Moved _getLatestMovies(); inside the FutureBuilder.

Improved one pasted below:

  @override
  Widget build(context) {
    var futureBuilder = FutureBuilder(
        future: _getLatestMovies(),
        builder: (context, snapshot) {
          if (snapshot.data != null) {
            return ListView.separated(
                padding: EdgeInsets.zero,

with this move, the FutureBuilder awaits the result from the method and passed onto the ListView as a snapshot.

Faisal
  • 1,332
  • 2
  • 13
  • 29