|
1 | 1 | ObjectBox Examples |
2 | 2 | ========================== |
3 | 3 |
|
| 4 | +Flutter |
| 5 | +-------- |
| 6 | +* See a [Flutter example app](flutter/objectbox_demo) for code examples - requires Flutter 1.12 |
| 7 | + |
| 8 | +As opposed to a plain Dart app which runs directly on your PC, there are more restrictions where your Flutter app can |
| 9 | +write data. Therefore, you should give ObjectBox a full path to a per-app documents directory, where to store the data |
| 10 | +even when a user closes your app. |
| 11 | + |
| 12 | +If you didn't specify this path to ObjectBox, it would try to use a default "objectbox" directory where the app is |
| 13 | +currently running, but it doesn't have permissions to write there: `failed to create store: 10199 Dir does not exist: objectbox (30)`. |
| 14 | + |
| 15 | +To configure ObjectBox properly, you can use `getApplicationDocumentsDirectory()` from the `path_provider` package. |
| 16 | +See [Flutter: read & write files](https://flutter.dev/docs/cookbook/persistence/reading-writing-files) for more info. |
| 17 | +Have a look how it's done in the Flutter example app: |
| 18 | +```dart |
| 19 | +import 'package:path_provider/path_provider.dart'; |
| 20 | +
|
| 21 | +class _MyHomePageState extends State<MyHomePage> { |
| 22 | + Store _store; |
| 23 | +
|
| 24 | + @override |
| 25 | + void initState() { |
| 26 | + super.initState(); |
| 27 | + |
| 28 | + getApplicationDocumentsDirectory().then((dir) { |
| 29 | + _store = Store(getObjectBoxModel(), directory: dir.path + "/objectbox"); |
| 30 | +
|
| 31 | + // See below as well as examples/flutter/objectbox_demo for examples how to use objectbox |
| 32 | + }); |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Dart native |
| 38 | +----------- |
4 | 39 | In the following file, e.g. `models.dart`, we import objectbox.dart to get definitions for `@Entity`, |
5 | 40 | `@Id` and other annotations and define a single entity that should be persisted by ObjectBox. You could have multiple |
6 | 41 | entities in the same file or you can have them spread across multiple files in the `lib` directory tree. |
@@ -51,34 +86,3 @@ void main() { |
51 | 86 | store.close(); |
52 | 87 | } |
53 | 88 | ``` |
54 | | - |
55 | | -Flutter |
56 | | --------- |
57 | | -* See a [Flutter example app](flutter/objectbox_demo) - requires Flutter 1.12 |
58 | | - |
59 | | -As opposed to a plain Dart app which runs directly on your PC, there are more restrictions where your Flutter app can |
60 | | -write data. Therefore, you should give ObjectBox a full path to a per-app documents directory, where to store the data |
61 | | -even when a user closes your app. |
62 | | - |
63 | | -If you didn't specify this path to ObjectBox, it would try to use a default "objectbox" directory where the app is |
64 | | -currently running, but it doesn't have permissions to write there: `failed to create store: 10199 Dir does not exist: objectbox (30)`. |
65 | | - |
66 | | -To configure ObjectBox properly, you can use `getApplicationDocumentsDirectory()` from the `path_provider` package. |
67 | | -See [Flutter: read & write files](https://flutter.dev/docs/cookbook/persistence/reading-writing-files) for more info. |
68 | | -Have a look how it's done in the Flutter example app: |
69 | | -```dart |
70 | | -import 'package:path_provider/path_provider.dart'; |
71 | | -
|
72 | | -class _MyHomePageState extends State<MyHomePage> { |
73 | | - Store _store; |
74 | | -
|
75 | | - @override |
76 | | - void initState() { |
77 | | - super.initState(); |
78 | | - |
79 | | - getApplicationDocumentsDirectory().then((dir) { |
80 | | - _store = Store(getObjectBoxModel(), directory: dir.path + "/objectbox"); |
81 | | - }); |
82 | | - } |
83 | | -} |
84 | | -``` |
|
0 commit comments