Search code examples
flutterservicerepositoryblocprovider

What is the difference between a provider, a repository and a service?


I've been getting back to Flutter BLOC lately and I really struggle with some notions I've encountered and I can't find any reliable resources to make it more clear except a 13 years old SO post, a bit old.

Can you help me understand what are :

  • Providers
  • Repositories
  • Services

And how they interact with each others?


Solution

  • This is tricky to answer simply because the terms means different things in different contexts. Some of them can mean the same thing based on who you ask and which design pattern being referred to. And depending on which design pattern you refer to, then the differences between the terms might be subtle. The terms are also very broad, so you'd have to define what each term mean in each context.

    Since you mention the words Flutter and BLOC after each other, I could assume (perhaps wrongly) that you are thinking about the flutter_bloc package. In that case, refer to the explanations and terminology of the official documentation. In this documentation the term service isn't mentioned by word.

    To broaden my answer and also include the term services, I'd say that, Provider and Repository could mean the same thing, and Repository and Service could mean the same thing. But I've never seen where Provider and Service could mean the same thing.

    Data Providers / Repositories

    Classes that communicate with e.g. databases etc and provide data from sources. These add some form of abstraction layer ontop of the data source(s).

    Repositories / Services

    Classes that wrap one or more data providers, or wrap one or more repositories when the wrapper is called a service. Could include additional business logic and mapping of raw data to domain objects.

    Additional meaning of Provider

    The term Provider is also used in the Flutter widget context when it comes to providing information down the widget tree. E.g. the BlocProvider from the flutter_bloc packages or the Provider from the provider package.

    Summary

    The key part, whether you call the things Providers, Repositories, Services, Apples or Bananas, is to separate the UI, the business logic, and the data fetching.