title: How to call a function in cubit class in initState?
body: <p>So, I have made a function in Cubit Class it is to get data from API. For now I just can get the data if I pressed a button. I wanna make the function automatically called when the page/screen is open. For your information, this page is the first page that will be launched when user open the app. Here is some of my codes.</p>
  <pre><code>class UsersCubit extends Cubit&lt;UsersState&gt; {
    UsersCubit() : super(UsersInitial());
  
    UserRepository _userRepository = UserRepository();
  
    void getAllUsers() async{
      emit(UsersLoading());
      try{
        ResponseUsers _data = await _userRepository.getUsers();
        emit(UsersSuccess(_data));
      } catch(e){
        emit(UsersError(e.toString()));
      }
    }
  }
  </code></pre>
  <pre><code>class UsersPage extends StatefulWidget {
    const UsersPage({Key? key}) : super(key: key);
  
    @override
    _UsersPageState createState() =&gt; _UsersPageState();
  }
  
  class _UsersPageState extends State&lt;UsersPage&gt; {
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text(&quot;Users&quot;)),
        body: BlocProvider(
          create: (context) =&gt; UsersCubit(),
          child: BlocConsumer&lt;UsersCubit, UsersState&gt;(
            listener: (context, state) {
              if(state is UsersLoading){
                print(&quot;getting users ...&quot;);
              } else if (state is UsersSuccess){
                print(state.data.users[1].identity!.name);
              } else if (state is UsersError){
                print(state.errorMessage);
              }
            },
            builder: (context, state) {
              return Stack(
                children: [
                  (state is UsersSuccess) ? listViewUsers(state.data.users) : progressBar(),
                  ElevatedButton(
                    onPressed: (){
                      context.read&lt;UsersCubit&gt;().getAllUsers();
                    },
                    child: Text(&quot;GET USERS&quot;),
                  )
                ],
              );
            },
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>I have tried to call the function directly in <code>initState</code> but when I run the app it returns an error.</p>
  <pre><code>  @override
    void initState() {
      context.read&lt;UsersCubit&gt;().getAllUsers();
      super.initState();
    }
  </code></pre>
  <p>error:</p>
  <pre><code>Error: Could not find the correct Provider&lt;UsersCubit&gt; above this UsersPage Widget
  
  This happens because you used a `BuildContext` that does not include the provider
  of your choice. There are a few common scenarios:
  
  - You added a new provider in your `main.dart` and performed a hot-reload.
    To fix, perform a hot-restart.
  
  - The provider you are trying to read is in a different route.
  
    Providers are &quot;scoped&quot;. So if you insert of provider inside a route, then
    other routes will not be able to access that provider.
  
  - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
  
    Make sure that UsersPage is under your MultiProvider/Provider&lt;UsersCubit&gt;.
    This usually happens when you are creating a provider and trying to read it immediately.
  
    For example, instead of:
  
    ...
    Widget build(BuildContext context) {
      return Provider&lt;Example&gt;(
        create: (_) =&gt; Example(),
        // Will throw a ProviderNotFoundError, because `context` is associated
        // to the widget that is the parent of `Provider&lt;Example&gt;`
        child: Text(context.watch&lt;Example&gt;()),
      ),
    }
    ...
  
    consider using `builder` like so:
  
    ...
    Widget build(BuildContext context) {
      return Provider&lt;Example&gt;(
        create: (_) =&gt; Example(),
        // we use `builder` to obtain a new `BuildContext` that has access to the provider
        builder: (context) {
          // No longer throws
          return Text(context.watch&lt;Example&gt;()),
        }
      ),
    }
  </code></pre>
  <p>Is there any way to solve this case?</p>