title: How to Backup and Restore Flutter Hive Data using JSON format?
body: <p>I want to create a password manager using Hive and have an option to backup the data or send the backup file to another device to copy the data to a new device. I was able to backup my data to a JSON file successfully.</p>
  <p>My model class:</p>
  <pre><code>import 'package:hive/hive.dart';
  part 'password.g.dart';
  
  @HiveType(typeId: 0)
  class Password extends HiveObject {
    @HiveField(0)
    late String website;
    @HiveField(1)
    late String email;
    @HiveField(2)
    late String pd;
  
  Map&lt;String, String&gt; toJson() =&gt; {
      'website': website,
      'email': email,
      'pd': pd,
    };
  }
  </code></pre>
  <p>To create a backup I used:</p>
  <pre><code>Future&lt;void&gt; createBackup() async {
      if (Hive.box&lt;Password&gt;('passwords').isEmpty) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('No Password Stored.')),
        );
        return;
      }
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Creating backup...')),
      );
      Map&lt;String, dynamic&gt; map = Hive.box&lt;Password&gt;('passwords')
          .toMap()
          .map((key, value) =&gt; MapEntry(key.toString(), value));
      String json = jsonEncode(map);
      Directory dir = await _getDirectory();
      String formattedDate = DateTime.now()
          .toString()
          .replaceAll('.', '-')
          .replaceAll(' ', '-')
          .replaceAll(':', '-');
      String path = '${dir.path}$formattedDate.json';
      File backupFile = File(path);
      await backupFile.writeAsString(json);
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Backup saved in folder Passman')),
      );
    }
  
    Future&lt;Directory&gt; _getDirectory() async {
      const String pathExt = '/Passman/';
      Directory newDirectory = Directory('/storage/emulated/0/' + pathExt);
      if (await newDirectory.exists() == false) {
        return newDirectory.create(recursive: true);
      }
      return newDirectory;
    }
  </code></pre>
  <p>Running this code gave me a .json backup file in a /Passman/ folder in root but now I have no idea how to use that file to restore the data back to the Hive.</p>