title: how to save data in Hive database when receiving data in the background?
body: <p>I have an issue saving data to Hive when receiving Firebase Cloud Messaging (FCM) push notification data when the app is in the background.</p>
  <p>I have a static method to set up hive like this</p>
  <pre><code>static Future&lt;void&gt; setUpHive() async {
      try {
  
        await Hive.initFlutter();
  
        if (!Hive.isBoxOpen(&quot;Box Name&quot;)) {
            await Hive.openBox(&quot;Box Name&quot;);
        }
  
      } catch (error) {
        print(error.toString());
      }
  }
  </code></pre>
  <p>I use that <code>setUpHive</code> static method in <code>main</code> function like this</p>
  <pre><code>Future&lt;void&gt; main() async {
  
    await HiveHelper.setUpHive();
  
    runApp(
      MyApp(),
    );
  }
  </code></pre>
  <p>when the app is in the background, and then it receives FCM message, then this code below will be called. after that I try change the data stored in the Hive box</p>
  <pre><code>Future&lt;void&gt; firebaseMessagingBackgroundHandler(RemoteMessage message) async {
      // when receive FCM message when app is in the background, this block will be executed
      
      // set up the hive first
      await HiveHelper.setUpHive(); 
  
      // then I try to change the data stored in the Hive box
      final myBox = Hive.box(&quot;BOX NAME&quot;);
      myBox.put(&quot;key&quot;, 12345);
  }
  </code></pre>
  <p>it seems okay after receiving FCM background data, but when I fully close the app, and the <code>main</code> called again I have error when trying to open the box like this</p>
  <pre><code>   static Future&lt;void&gt; setUpHive() async {
          try {
  
            await Hive.initFlutter();
  
            if (!Hive.isBoxOpen(&quot;Box Name&quot;)) {
                await Hive.openBox(&quot;Box Name&quot;); // Error in this line
            }
  
          } catch (error) {
            print(error.toString());
          }
      }
  </code></pre>
  <p>the error is:</p>
  <blockquote>
  <p>HiveError: This should not happen. Please open an issue on GitHub.
  E/flutter (13142): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)]
  Unhandled Exception: HiveError: This should not happen. Please open an
  issue on GitHub. E/flutter (13142): #0      BinaryReaderImpl.readFrame
  (package:hive/src/binary/binary_reader_impl.dart:250:7)
  E/flutter
  </p>
  </blockquote>
  <p>I try to find the solution, and I find similar issue from <a href="https://github.com/hivedb/hive/issues/122" rel="nofollow noreferrer">here about Using Hive DB in a Background Process</a> and it is said</p>
  <blockquote>
  <p>leisim:</p>
  <p>Unfortunately, Hive does not support opening boxes in multiple
  isolates. That means you can either close the box in the main isolate,
  update it in your background isolate and reopen it in the main isolate
  or you pass the data from the background to the main isolate and
  perform the update there...</p>
  </blockquote>
  <p>I am new in Flutter, and I don't understand what he said. please help :(</p>