How to conditionally import hive_flutter (so I can run the file from the command line)
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**Question**
I'm building a Flutter app, and would like to use Hive as the storage mechanism.
I'm trying to decouple the storage code from the interface code, so that:
* Testing will be faster/easier - I don't have to load all of the Flutter libraries and run the code on an emulator, I can just test the code as a command line Dart program
* It will be portable and reusable outside of Flutter
Is there a way that I can only import 'hive_flutter' if the file is being run in the context of a Flutter app?
Currently, if I try to run the file as a Dart command line program, the 'import hive_flutter' seems to look for libraries that should be loaded if Flutter is being used, and then errors out:
```
lib/models/base_record.dart: Warning: Interpreting this as package URI, 'package:ev/models/base_record.dart'.
../../../../dev_tools/flutter/packages/flutter/lib/src/foundation/basic_types.dart:10:1: Error: Not found: 'dart:ui'
export 'dart:ui' show VoidCallback;
```
[This example](https://stackoverflow.com/questions/58710226/how-to-import-platform-specific-dependency-in-flutter-dart-combine-web-with-an) is close, but it seems to assume you're either running your code in the context of (Flutter for Mobile || Flutter for Web). I want to be able to detect (Flutter || Not Flutter)...if that makes sense.
Thanks for the help!
**Code sample - Sort of**
```dart
// Something like:
import "package:hive/hive.dart";
if(partOfAFlutterAppBuild) {
import "package:hive_flutter/hive_flutter.dart";
}
// Then
if(isAFlutterApp) {
Hive.initFlutter();
} else {
Hive.init();
}
```
**Version**
- Platform: iOS, Android, Mac
- Flutter version: 1.12.13+hotfix.9
- Hive version: latest
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.