title: Flutter FutureBuilder shows data then disappears when calling Rest API (Backend Laravel)
body: <p>I'm running a flutter application with a laravel backend and I have some issues.</p>
  <p>The problem is the FutureBuilder show data then it disappears;  Sometimes length==4 then it turns to 0 and shows 'no data' in Scaffold</p>
  <p>The same when I refresh the code.</p>
  <p>PS: I'm running laravel on localhost and using a real device to test.</p>
  <p>Environment: Android Studio, Windows 10, Real device</p>
  <p>Laravel project: <a href="https://github.com/brakenseddik/blog_api_laravel" rel="nofollow noreferrer">https://github.com/brakenseddik/blog_api_laravel</a></p>
  <p>Flutter project: <a href="https://github.com/brakenseddik/blog_api_flutter" rel="nofollow noreferrer">https://github.com/brakenseddik/blog_api_flutter</a>
  <a href="https://i.stack.imgur.com/gXOYP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gXOYP.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/msyeq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/msyeq.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/PmrF5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PmrF5.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/UoY8S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UoY8S.png" alt="enter image description here" /></a>
  import 'package:http/http.dart' as http;</p>
  <pre><code>class Repository {
    String _baseUrl = 'http://192.168.1.2:8000/api';
    httpGet(String api) async {
      return await http.get(_baseUrl + '/' + api);
    }
  }
  </code></pre>
  <p>here the homepage source code</p>
  <pre><code> import 'dart:convert';
  
  import 'package:blog_api/models/post_model.dart';
  import 'package:blog_api/services/post_service.dart';
  import 'package:flutter/material.dart';
  
  class HomeScreen extends StatefulWidget {
    @override
    _HomeScreenState createState() =&gt; _HomeScreenState();
  }
  
  class _HomeScreenState extends State&lt;HomeScreen&gt; {
    PostService _postService = PostService();
    List&lt;PostModel&gt; _list = List&lt;PostModel&gt;();
  
    Future&lt;List&lt;PostModel&gt;&gt; _getPosts() async {
      var result = await _postService.getAllPosts();
      _list = [];
      if (result != null) {
        var blogPosts = json.decode(result.body);
        blogPosts.forEach((post) {
          PostModel model = PostModel();
          setState(() {
            model.title = post['title'];
            model.details = post['details'];
            model.imageUrl = post['featured_image_url'];
            _list.add(model);
          });
        });
      }
  
      return _list;
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          appBar: AppBar(
            title: Text('Blog App'),
          ),
          body: FutureBuilder(
            future: _getPosts(),
            builder:
                (BuildContext context, AsyncSnapshot&lt;List&lt;PostModel&gt;&gt; snapshot) {
              print('length of list ${_list.length}');
              _list = snapshot.data;
              if (_list.length == 0) {
                return Center(
                  child: Text('No data'),
                );
              } else if (!snapshot.hasData) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              } else {
                return ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Card(
                          child: Column(
                            children: [
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Image.network(
                                  snapshot.data[index].imageUrl,
                                  height: 150,
                                  //  width: double.maxFinite,
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Text(
                                  snapshot.data[index].title,
                                  style: TextStyle(
                                      fontSize: 18, fontWeight: FontWeight.w700),
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Text(
                                  snapshot.data[index].details.substring(0, 25),
                                  style: TextStyle(
                                    fontSize: 16,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    });
              }
            },
          ));
    }
  }
  </code></pre>
  <p>And the PostService</p>
  <pre><code>import 'package:blog_api/repository/repository.dart';
  
  class PostService {
    Repository _repository;
    PostService() {
      _repository = Repository();
    }
    getAllPosts() async {
      return await _repository.httpGet('get-posts');
    }
  }
  </code></pre>