isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.1k stars 407 forks source link

Hive init on WEB #224

Closed danieramiz closed 4 years ago

danieramiz commented 4 years ago

Question I'm trying to implement Hive in my Flutter web project, but I have issues with the init method, this supposed to work with getApplicationDocumentsDirectory, but this is not supported for path_provider on WEB. Is there any alternative?

Code sample

  var dir = await getApplicationDocumentsDirectory();
  Hive.init(dir.path);
  Hive.registerAdapter( ClientAdapter()); 
  Hive.registerAdapter(ProductAdapter()); 
  return await Hive.openBox('exampleBox');

Version

simc commented 4 years ago

You don't need to call Hive.init() on the web. If you use the hive_flutter package, you can use Hive.initFlutter() which will internally call Hive.init() only if necessary.

gmukul01 commented 4 years ago

@leisim

I am trying to use Hive for flutter web but I am getting below error

Future main() async { Hive.initFlutter(); runApp(App()); }

ERROR:

Uncaught (in promise) Error: Unsupported operation: Platform._operatingSystem
    at Object.throw_ [as throw] (errors.dart:196)
    at Function._operatingSystem (io_patch.dart:241)
    at Function.get operatingSystem [as operatingSystem] (platform_impl.dart:62)
    at get _operatingSystem (platform.dart:73)
    at Function.desc.get [as _operatingSystem] (utils.dart:77)
    at get isWindows (platform.dart:129)
    at Function.desc.get [as isWindows] (utils.dart:77)
    at new backend_manager.BackendManager.new (backend_manager.dart:9)
    at new hive_impl.HiveImpl.new (hive_impl.dart:28)
    at get Hive (hive.dart:41)
    at Object.desc.get [as Hive] (utils.dart:77)
    at main$ (main.dart:14)
    at main$.next (<anonymous>)
    at runBody (async_patch.dart:86)
    at Object._async [as async] (async_patch.dart:125)
    at Object.main$ [as main] (main.dart:11)
    at main$ (web_entrypoint.dart:5)
    at main$.next (<anonymous>)
    at onValue (async_patch.dart:47)
    at _RootZone.runUnary (zone.dart:1381)
    at _FutureListener.thenAwait.handleValue (future_impl.dart:140)
    at handleValueCallback (future_impl.dart:682)
    at Function._propagateToListeners (future_impl.dart:711)
    at async._AsyncCallbackEntry.new.callback (future_impl.dart:391)
    at Object._microtaskLoop (schedule_microtask.dart:43)
    at _startMicrotaskLoop (schedule_microtask.dart:52)
    at async_patch.dart:168
muhleder commented 4 years ago

Seeing the same, after upgrading to latest flutter master and Hive 1.4 from 1.3. I don't think Platform.isWindows has ever been supported on the web?

This change fixes the immediate error for me.

https://github.com/muhleder/hive/commit/62a925900dfc59f33c2385cbe2a65b0e22c40d82

But then I get an error when trying to open the box. Looks like a recent flutter change is causing the vm backend manager to be used instead of the js backend manager. Seeing the same issue on 1.3 as well which was working for me on an earlier flutter release.

muhleder commented 4 years ago

Looks like conditional imports have stopped working on the web,

import 'none.dart'
if (dart.library.io) 'io.dart'
if (dart.library.html) 'web.dart';

doesn't work anymore, though reversing the order seems to get it working.

import 'none.dart'
if (dart.library.html) 'web.dart'
if (dart.library.io) 'io.dart';
simc commented 4 years ago

Could you please post the output of flutter doctor -v, it works fine for me with flutter from the master channel.

muhleder commented 4 years ago

Marks-MacBook-Pro:demo mark$ flutter doctor -v [✓] Flutter (Channel unknown, v1.15.4-pre.68, on Mac OS X 10.14.6 18G103, locale en-GB) • Flutter version 1.15.4-pre.68 at /opt/flutter • Framework revision 85ab331c82 (4 hours ago), 2020-02-14 10:41:23 +0100 • Engine revision 0e8ed2715a • Dart version 2.8.0 (build 2.8.0-dev.8.0 5244d99a5d)

[!] Android toolchain - develop for Android devices • Android SDK at /usr/local/Caskroom/android-platform-tools/29.0.4 • Android NDK location not configured (optional; useful for native profiling support) ✗ Unable to locate Android SDK. Install Android Studio from: https://developer.android.com/studio/index.html On first launch it will assist you in installing the Android SDK components. (or visit https://flutter.dev/setup/#android-setup for detailed instructions). If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location. You may also want to add it to your PATH environment variable.

✗ No valid Android SDK platforms found in /usr/local/Caskroom/android-platform-tools/29.0.4/platforms. Directory was empty.
• Try re-installing or updating your Android SDK,
  visit https://flutter.dev/setup/#android-setup for detailed instructions.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 11.3.1, Build version 11C504 • CocoaPods version 1.7.5

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed) • Android Studio not found; download from https://developer.android.com/studio/index.html (or visit https://flutter.dev/setup/#android-setup for detailed instructions).

[✓] Connected device (3 available) • macOS • macOS • darwin-x64 • Mac OS X 10.14.6 18G103 • Chrome • chrome • web-javascript • Google Chrome 80.0.3987.106 • Web Server • web-server • web-javascript • Flutter Tools

! Doctor found issues in 2 categories.

simc commented 4 years ago

Strange, I'm on the same build. Do you have a small code sample that reproduces the error? The unit tests should have reported a problem if Hive was not working with the stable or dev version of Dart.

muhleder commented 4 years ago

Just Hive.initFlutter() in the app from flutter create myApp is enough to do it.

muhleder commented 4 years ago

The error is caused by the conditional export here https://github.com/hivedb/hive/blob/master/hive/lib/src/backend/storage_backend.dart#L5-L7

muhleder commented 4 years ago

For some reason on my flutter build it's picking up the vm import instead of going to the web version. if (dart.library.io) 'package:hive/src/backend/vm/backend_manager.dart'

muhleder commented 4 years ago

Running the app with flutter run -d chrome on OSX

simc commented 4 years ago

Thank you! I can reproduce the problem now. It seems to only happen in actual flutter apps. It works fine with a Dart program using the Flutter version of Dart.

muhleder commented 4 years ago

It was working 20 days ago for me on master on this commit https://github.com/flutter/flutter/commit/93a5b7d419d764bfcad2fea25ab6dc62d39f401a I'm going to bisect it and find where it breaks.

simc commented 4 years ago

I can confirm that it works with the commit you mentioned. I don't think there is anything we can do to solve this within Hive. We'll need to file a Flutter issue.

danieramiz commented 4 years ago

Hey, guys, you attended my questions very quickly. Thank you. So we need to wait for a new release of Hive or Flutter?

Thanks

muhleder commented 4 years ago

The commit in flutter that breaks conditional imports is this one, https://github.com/flutter/flutter/commit/18f38cd45bee65931c28433c2071d48570b62b1b so you could use a commit from before that point. Also, release mode still seems to work, the bug only seems to affect debug mode.

danieramiz commented 4 years ago

thank you

simc commented 4 years ago

The commit in flutter that breaks conditional imports is this one, flutter/flutter@18f38cd

Thanks for your help, finding the problem. Do you want to file a Flutter issue or should I do it?

muhleder commented 4 years ago

I've just filed one https://github.com/flutter/flutter/issues/50801 Feel free to add any clarifications.

rydmike commented 4 years ago

I just also noticed that Hive is still broken on Flutter master Web builds due to this issue.

I had a number of own similar conditional imports as well in my own apps that started to fail after 'dart.io' suddenly started to be reported as supported on Flutter master Web builds and get imported on Web builds and subsequently the app started to hang on unsupported calls in the imported file(s).

I managed to temporarily fix my own stuff by adding the silly extra check: if (!kIsWeb) { } around all function calls in the imported files that should not have been imported on Web builds in the first place, before any calls that would bomb on web. So now my own stuff works on stable, dev and master again, but my Hive usage still hangs on Flutter master Web builds.

I also reported the conditional import issue: https://github.com/flutter/flutter/issues/51290 Later @muhleder I noticed your report as well and added a mention to it in my report.

I tweeted about the issue here: https://twitter.com/RydMike/status/1231343976311070722 Managed to at least get Eric Seidel and Kevin Moore to see it, so maybe it will get some action next week.

muhleder commented 4 years ago

Looks like the dart commit https://dart-review.googlesource.com/c/sdk/+/136411 didn't make it in to the last engine dart autoroll https://github.com/flutter/engine/pull/16716

Once that gets rolled into engine, and then that version of the engine gets rolled into flutter it sounds like it should be fixed.

rydmike commented 4 years ago

Thanks good to know, let's hope it happens soon, this is a fairly annoying regression.

muhleder commented 4 years ago

Fixed as of this commit in flutter master https://github.com/flutter/flutter/pull/51590

simc commented 4 years ago

Great :+1: