6

Im experimenting with flutter, and ive came up with many projects on github that declare the repositories and pass them to the bloc on the UI side. Why is that the proper way to do it and not on the backend side (on the bloc) ?

Example:

class MyApp extends StatelessWidget {
  final authenticationRepository = AuthenticationRepository();
  final accountRepository = AccountRepository();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      debugShowCheckedModeBanner: false,
      home: MultiRepositoryProvider(
        providers: [
          RepositoryProvider(create: (_) => authenticationRepository),
          RepositoryProvider(create: (_) => accountRepository),
        ],
        child: BlocProvider(
          create: (_) => AuthenticationBloc(
            authenticationRepository: authenticationRepository,
            accountRepository: accountRepository,
          ),
          child: Container(); // I removed the content here
          ),
        ),
      ),
    );
  }
}

Thank you

RedZ
  • 857
  • 1
  • 13
  • 25

2 Answers2

4

It is not about "the UI" side, but more about the thing that sits above UI and BLOCs. Remember, that in Flutter everything is a Widget. So some Widget will need to instantiate and dispose the repository and from my experience this is most likely a widget at the very top.

You ask "Why not create it in the Bloc?"

We don't do this, because that would violate inversion of control / dependency inversion principle and would make the blocs harder to test. We want to inject the blocs dependencies from outside, so we can control the dependency in a unit test.

Another reason can be that repositories are used in several places (e.g. by multiple different blocs or multiple instances of the same bloc). The repository instance probably should not change, while blocs are created on demand in the widget tree.

Stuck
  • 11,225
  • 11
  • 59
  • 104
  • Can't we create a single repository instance throughout a flutter application, avoiding the repository provider? Because the reference to the repository has to be made anyway. – Anoop Thiruonam Feb 17 '23 at 06:28
3

BLoC was created because developers were looking for a way to use the same business logic in dart applications of different platforms. For this to work, BLoCs should be implemented independently of the underlying platform.

Usually BLoCs access data using repository interfaces whose implementation can be platform-specific. Therefore, they are injected into the BLoC from the outside (e.g. from Flutter or AngularDart).

Paolo Soares came up with BLoC and he explains it here: https://www.youtube.com/watch?v=PLHln7wHgPE

Tim Brückner
  • 1,928
  • 2
  • 16
  • 27
  • What you say here about Blocs usually using the repo interface aligns with my current (limited) understanding but I am following Flutter App with Clean Architecture and the BloC Pattern course by Massimo Del Pezzo and the code uses RepositoryProvider to create the repos based on the implementation, not the interface. I'm no expert but this feels wrong. Are there good reasons for doing this? – macasas May 08 '23 at 08:51
  • 1
    Yes, pragmatism. If you share code between different systems I suggest using interfaces. If there is nothing to be shared (wich is often the case) an interface is usually boilerplate, because there is only one implementation. – Tim Brückner May 11 '23 at 13:04