title: Flutter dart export hive saved data to file to retrieve later
body: <p>I am developing a barcode app and save the data to hive.  What I need to know is there a way to export the saved hive database to a backup file and be able to retrieve it for instance if the app crashed or your phone is lost.  This is for blind accessibility.  Want to export the data to a file that I can save to my pc to store and if something happens I do not have to scan all the products again to build the database.  If hive can not do this can someone point me in a direction of which flutter dart database can do this.  Thank you</p>
  <p>Ok the answer did not work for me.  Here is a copy of my model file</p>
  <pre><code>    import 'package:hive/hive.dart';
  
      part 'product.g.dart';
      @HiveType(typeId: 0)
      class Product extends HiveObject{
        @HiveField(0)
        String itemName;
        @HiveField(1)
        String barCode;
        @HiveField(2)
        String bcType;
  
        Product(this.itemName, this.barCode, this.bcType);
      }
  </code></pre>
  <p>Then I call my box like
  var box = Hive.box('products');</p>
  <p>How to encode this to json for saving?</p>
  <p>I use the next</p>
  <pre><code>    Future&lt;File&gt; _createBackupFile() async {
        /// This example uses the OS temp directory
  </code></pre>
  <p>File backupFile =         File('${Directory.systemTemp.path}/backup_barcode.json');</p>
  <pre><code>      try {
          /// barcodeBox is the [Box] object from the Hive package, usually exposed inside a [ValueListenableBuilder] or via [Hive.box()]
      var barcodeBox = Hive.box&lt;Product&gt;('products');
         backupFile = await backupFile.writeAsString(jsonEncode(barcodeBox.values));
  
          return backupFile;
        } catch (e) {
          // TODO: handle exception
      print(e);
        }
      }
  </code></pre>