Coder Social home page Coder Social logo

Load Hive from flutter assets about hive HOT 3 CLOSED

isar avatar isar commented on July 21, 2024
Load Hive from flutter assets

from hive.

Comments (3)

ershat-dl avatar ershat-dl commented on July 21, 2024 1

I found a solution, but not directly.

  • first: I copied existing hive file mac2factory.hive into my assets folder, and define it in pubspec.yaml, like above:
    assets/mac2factory.hive
  • second: I load mac2factory.hive in flutter by using PlatformAssetBundle().load;;;
    final mac2factoryByte = await PlatformAssetBundle().load('assets/mac2factory.hive');
  • third: open target file in flutter app while runnig
    var applicationDocumentsDirectory = await getApplicationDocumentsDirectory();
    File file = File('${applicationDocumentsDirectory.path}/mac2factory.hive');
  • four: write mac2factoryByte file, it's byteData type, so:
await file.writeAsBytes(mac2factoryByte.buffer.asUint8List(mac2factoryByte.offsetInBytes, mac2factoryByte.lengthInBytes));

I could do this for now, can't find any solution.

from hive.

simc avatar simc commented on July 21, 2024

It is important that every box has a file where changes can be stored.

I can't think of a way to avoid copying a box file from the assets to an actual directory.

We could implement something like this in the hive_flutter package but I'm not sure if that's a good idea.

I would create a helper method for your app.

from hive.

olegbilovus avatar olegbilovus commented on July 21, 2024

If you need to read only the content you can use the following:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();
  final bytesDatabase = await rootBundle.load('hive/database.hive');
  await Hive.openBox('database', bytes: bytesDatabase.buffer.asUint8List());
  
  runApp(const MyApp());
}

This will create a copy of the database in assets to getApplicationDocumentsDirectory().
It was tested on Android, Web and Windows.

from hive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.