0

In application background state, I want to fetch some data from API then store them into Hive database. for running task in background, I use workmanager package and added necessary code to fetch data from API and save that into Hive database. But it returns me error everytime.

LateInitializationError: Field '_my_hive_Box@66472566' has not been initialized.

this saying my hive box is not initialized. But I initialize them in the main()

is there any way to solve this hive box initialization?

class MyHive {
  // prevent making instance
  MyHive._();

  // hive box to store Client user  data
  static late Box<SellClientSyncModel> _clientBox;

  // store current user as (key => value)
  static const String _currentUserKey = 'local_user';

  /// initialize local db (HIVE)
  /// pass testPath only if you are testing hive
  static Future<void> init(
      {Function(HiveInterface)? registerAdapters, String? testPath}) async {
    if (testPath != null) {
      Hive.init(testPath);
    } else {
      await Hive.initFlutter();
    }
    await registerAdapters?.call(Hive);
    await initUserBox();
  }

  /// initialize user box
  static Future<void> initUserBox() async {
    _clientBox = await Hive.openBox(AppConstant.clientBoxHive);
  }

  /// Client Methods
  static Future<bool> saveClientToHive(SellClientSyncModel user) async {
    try {
      await _clientBox.put(_currentUserKey, user);
      return true;
    } catch (error) {
      print(error);
      return false;
    }
  }
}

in main()

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // initialize hive for whole app
  await MyHive.init(registerAdapters: (hive) {
    hive.registerAdapter(SellClientAdapter());
  });

  runApp(child: MyApp());

}
3
  • please show your code, how you have initialized it Commented Aug 12, 2024 at 14:56
  • 1
    Dart doesn't support sharing memory between isolates. you can use send and receive port Commented Aug 12, 2024 at 15:19
  • when should I use send port ? before starting background task on workmanager? another thing I need to know, shared_preferences supports shared memory, that's why I could use that. Is Sqflite use shared memory, so that I could use sqflite instead of hive Commented Aug 12, 2024 at 15:35

1 Answer 1

0

Check this two link:
solution 1
solution 2

if you can't understant Send and Receive port. A very easy way is to load incoming API response and save them into a response.txt file. Then you can extract those local response into Object.

This is how you can save in local .txt file

void saveResponseToJsonFile(dynamic data) async {
    // Define the file path (ensure the path exists)
    Directory directory = await getApplicationDocumentsDirectory();
    String filePath = '${directory.path}/response.txt';

    File file = File(filePath);

    // Write the JSON string to the file
    await file.writeAsString(data.toString());
    
  }

must use path_provider package..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.