title: keeping repository instance "alive" with bloc
body: <p>I am still with my first bloc based app, adding features. While previously, I stored some of my page specific data with the bloc class, for the last feature, I now moved  most variables into its repository. I already feared that the instance of calling the repository gets lost, afterwards, which now proved true.</p>
  <p>Is there a proper, easy way to make the instance persistent?</p>
  <p>I know of inherited widgets, however, I have not yet figured out how to implement this and my <a href="https://stackoverflow.com/questions/64688230/didier-boelens-reactive-programming-bloc-or-how-to-implement-an-app-wide-bloc">question</a> around this unfortunately remained unanswered. It would be great, if someone could point me to some direction!</p>
  <p>In general, my idea was to have the api dealing with local files and online data, the repository with frequently re-used data (session data, presented data etc) and helper variables within the bloc. So when the UI requests data, the bloc asks the repository which will either return a value stored in a variable or request a value from the api.</p>
  <p>This is, how the strucuture basically looks like (hope I have not missed anything significant)</p>
  <pre class="lang-dart prettyprint-override"><code>void main() async {
    final UserRepository userRepository = UserRepository(); // &lt;===== userRepository initialized
    runApp(MyApp(userRepository: UserRepository()));
  }
  
  class MyApp extends StatelessWidget {
    MyApp({Key key, this.userRepository}) : assert(userRepository != null), super(key: key);
    final UserRepository userRepository;
  
    @override
    Widget build(BuildContext context) {
      return BlocProvider&lt;UserBloc&gt;(  &lt;====== userBloc injection to top of widget tree 
        create: (_) =&gt; UserBloc(userRepository: userRepository)..add(AppStarted()),
        child: App(),
      );
    }
  }
  // =================================================== APP WITH ROUTES
  class App extends StatelessWidget {
    App({Key key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return CupertinoApp(
        routes: {
          '/': (_) =&gt; HomePage(),
          'feature 1': (_) =&gt; HomePage(),
        },
      );
    }
  }
  // =================================================== LANDING PAGE WITH MAIN MENU
  class HomePage extends StatefulWidget {
    HomePage({Key key, this.title}) : super(key: key);
    final String title;
    @override
    _HomePageState createState() =&gt; _HomePageState();
  }
  class _HomePageState extends State&lt;HomePage&gt;  { 
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
      ]);
      return CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          middle: Text('MathUup'),
        ),
        child: SafeArea(
          child: CupertinoButton(
            child: Text('Feature 1',
                onPressed: () =&gt; Navigator.pushNamed(context, 'feature 1'),
    ),)));
  }}     
  // =================================================== BLOC
  class UserBloc extends Bloc&lt;UserEvent, UserState&gt; {
    UserBloc({this.userRepository}) : super(AppInitial());
    final UserRepository userRepository;
    ...
      
        final user = await userRepository.getActiveUserData(userId);
        final lastSessionData = await userRepository.getLastSession(userId);
    ...
  }
  // =================================================== REPOSITORY
  class UserRepository {
    UserRepository();
    final UserApiClient achievementsApiClient = UserApiClient();
    final SessionsApiClient sessionsApiClient = SessionsApiClient();
    UserSession activeUserSession;
    User activeUserData;
    
    
    Future&lt;String&gt; getLastUserId() async {
      final lastUserId = await sessionsApiClient.getLastUserId();
      return lastUserId;
    }
    Future&lt;UserSession&gt; getActiveUser() async {
      if (activeUserSession == null) {
        activeUserSession = await sessionsApiClient.getLastUser();
      }
      return activeUserSession;
    }
  }
    
    
  </code></pre>