title: Why (usually) there's a repository layer on BLoC pattern?
body: <p>I'm new to Flutter and have just heard the BLoC concept from reading tutorials about Flutter. From this <a href="https://medium.com/flutterpub/architecting-your-flutter-project-bd04e144a8f1" rel="noreferrer">tutorial</a>, I heard BLoC for the first time. But I also see a file called "Repository" in this article. Basically the data flow goes like this:</p>
  
  <pre><code>Web API --&gt; Api Provider --&gt; Repository --&gt; BLoC --&gt; Widget
  </code></pre>
  
  <p>What I don't understand is that why there's a need for the Repository layer, as when I look at the repository file, it's basically just returning the API Provider's Future result? I got curious and try to search further, and I see some of the people's coding patterns on the internet also has a Repository layer on it. </p>
  
  <p>In the original article, the API Provider does everything. It calls the get request, it awaits for the Future resolve, it converts the JSON data into appropriate model, and return the model enclosed with Future.</p>
  
  <pre><code>class ApiProvider {
    Future&lt;ItemModel&gt; fetchMovieList() async {
      final response = await client.get("http://api.themoviedb.org/3/movie/popular?api_key=$_apiKey");
      if (response.statusCode == 200)
        return ItemModel.fromJson(json.decode(response.body));
      else
        throw Exception('Failed to load post');
    }
  }
  
  class Repository {
    ApiProvider _api = ApiProvider();
  
    Future&lt;ItemModel&gt; fetchMovieList() =&gt; _api.fetchMovieList(); // why?
  }
  
  class Bloc {
    Repository _repository = Repository();
    final _moviesFetcher = PublishSubject&lt;ItemModel&gt;();
    Observable&lt;ItemModel&gt; get allMovies =&gt; _moviesFetcher.stream;
  
    fetchAllMovies() async {
      ItemModel itemModel = await 
      _repository.fetchAllMovies();
      _moviesFetcher.sink.add(itemModel);
    }
  }
  </code></pre>
  
  <p>Currently I modify it so that the Api Provider returns pure Future, where the Repository implement the <code>.then()</code> and convert the response into appropriate data, but I tend to avoid await because in React Native await causes the app to look unresponsive. I also move error checking into BLoC.</p>
  
  <pre><code>class ApiProvider {
    Future fetchMovieList() =&gt; client.get("http://api.themoviedb.org/3/movie/popular?api_key=$_apiKey");
  }
  
  class Repository {
    ApiProvider _api = ApiProvider();
    Future&lt;ItemModel&gt; fetchMovieList() =&gt; _api.fetchMovieList().then(response =&gt; ItemModel.fromJson(json.decode(response.body));
  }
  
  class Bloc {
    Repository _repository = Repository();
    final _moviesFetcher = PublishSubject&lt;ItemModel&gt;();
    Observable&lt;ItemModel&gt; get allMovies =&gt; _moviesFetcher.stream;
  
    fetchAllMovies() async =&gt; _repository.fetchPopularMovies().then((response) =&gt; _moviesFetcher.sink.add(response))
    .catchError((onError) =&gt; throw Exception("Failed to load post $onError"));
  }
  </code></pre>
  
  <p>But still, I feel like this is a stretch to justify the need for this Repository layer. If I can, I want to make it like this:</p>
  
  <pre><code>class ApiProvider {
    Future&lt;ItemModel&gt; fetchMovieList() =&gt; client.get("http://api.themoviedb.org/3/movie/popular?api_key=$_apiKey")
      .then(response =&gt; ItemModel.fromJson(json.decode(response.body));
  }
  
  class Bloc {
    ApiProvider _api = ApiProvider();
    final _moviesFetcher = PublishSubject&lt;ItemModel&gt;();
    Observable&lt;ItemModel&gt; get allMovies =&gt; _moviesFetcher.stream;
  
    fetchAllMovies() async =&gt; _api.fetchPopularMovies().then((response) =&gt; _moviesFetcher.sink.add(response))
    .catchError((onError) =&gt; throw Exception("Failed to load post $onError"));
  }
  </code></pre>
  
  <p>and get rid of the Repository layer altogether. I'm not trying to say the Repository layer is unnecessary, but right now I don't know what pattern problem the Repository layer trying to solve. I just want to know why there's a Repository layer in the first place and what the real-world significant use case of Repository. I know this question may be flagged as a question that can trigger discussion instead of straight answers. But I believe there is some kind of narrowed answers for this question. I just can't find it when I tried to search on the internet (the search result got mixed up with other uses of "Repository" terms, like git and subversion).</p>