isar / isar/hive

Flutter: Hive files exposed to user on (theoricaly all) platforms and potentially common between different apps

Open
#1,044 2 comments 14 reactions 0 assignees View on GitHub
problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Steps to Reproduce**
Do `Hive.initFlutter()`

**Code sample**

Hive.initFlutter() is using `getApplicationDocumentsDirectory` which is designed to provide a folder accessible to users and therefor also "common between different apps" (source: "path_provider" package) !
(go to `More info` section to see the folders depending on the platform)

**Version**
- Platform: Mac, Windows, Linux, (iOS and Android theoricaly but not in practice even though it seems to impact some people: https://github.com/hivedb/hive/issues/840)
- Flutter version: [3.0.5]
- Hive version: [all, including 2.2.3]

**How to fix**

Use `getApplicationSupportDirectory` instead of `getApplicationDocumentsDirectory` or at least allow to use completly other folder at https://github.com/hivedb/hive/blob/3b12c31a221f97f5ec86fe20df63515aeedf88f0/hive_flutter/lib/src/hive_extensions.dart#L16

Other solution: Add a comment to explain the implication of initFlutter without using getApplicationSupportDirectory (but with this solution, most users will use the "default bad choice"...
Because most users should do :
```dart
import 'package:flutter/foundation.dart' show kIsWeb;

if (kIsWeb) {
await Hive.initFlutter();
} else {
await Hive.initFlutter((await getApplicationSupportDirectory()).path);
}
```
and not `await Hive.initFlutter();` but is really dirty as it implies we need to rely on `path.join(badDir, goodDir)` to understand what we mean... (this function is not intended this way...) https://github.com/hivedb/hive/blob/3b12c31a221f97f5ec86fe20df63515aeedf88f0/hive_flutter/lib/src/hive_extensions.dart#L17

**Ressources**

path_provider documentation:

```dart
/// Use this for files you don’t want exposed to the user. Your app should not
/// use this directory for user data files.
/// (...)
getApplicationSupportDirectory
```

```dart
/// Path to a directory where the application may place data that is
/// user-generated, or that cannot otherwise be recreated by your application.
/// On iOS, this uses the `NSDocumentDirectory` API. Consider using
/// [getApplicationSupportDirectory] instead if the data is not user-generated.
/// (...)
getApplicationDocumentsDirectory
```

**More Info**

Here are where data are stored depending on the function called:

getApplicationDocumentsDirectory:
- Windows: `C/Users/{username}/Documents/` (User accessible AND files common between apps 👎👎 )
- Linux: `/home/{user}/` (User accessible AND files common between apps 👎👎 )
- Android : `/data/user/0/{YourAppName}/app_flutter/` (Not really accessible so I guess it is "ok" & dependent on app 👍)
- MacOS (Not tested, but I guess, it is like Linux & Windows 👎👎 )
- IOS (Not tested, but from documentation, it is specified we should NOT use this function... 👎 )
- Web: Not using this function (👍 )

getApplicationSupportDirectory :
- Windows: `C:\Users\{user}\AppData\Roaming\{YourAppOrganization}\{YourAppName}/` (Not accessible & dependent on app 👍 )
- Linux: `/home/{user}/.local/share/{YourAppName]/` (Not accessible & dependent on app 👍 )
- Android: `/data/user/0/{YourAppName}/files/` (Not accessible & dependent on app 👍)
- MacOS (Not tested, but I guess, it is like Linux & Windows 👍 )
- IOS (Not tested, but from documentation, it is specified we SHOULD use this function... 👍 )
- Web: Not using this function (👍 )

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in hive_flutter/lib/src/hive_extensions.dart at Hive.initFlutter(), then compare the path_provider APIs cited in the issue. Verify the default storage location across the listed platforms and determine whether the default should use application support storage or expose a separate configurable path; completion should address the user-accessibility and cross-app-sharing concern.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.