Skip to content

Commit 2f8c43d

Browse files
committed
update examples to null-safety and accept --sync argument in install.sh
1 parent 3407d4f commit 2f8c43d

File tree

5 files changed

+47
-42
lines changed

5 files changed

+47
-42
lines changed

install.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ set -eu
1010
# * have a look at the changed files to see if some call sites need to be updated
1111
cLibVersion=0.13.0
1212
os=$(uname)
13+
cLibArgs="$*"
1314

1415
# if there's no tty this is probably part of a docker build - therefore we install the c-api explicitly
15-
cLibArgs=
16-
if [[ "$os" != MINGW* ]] && [[ "$os" != CYGWIN* ]]; then
16+
if [[ "$os" != MINGW* ]] && [[ "$os" != CYGWIN* ]] && [[ "$cLibArgs" != *"--install"* ]]; then
1717
tty -s || cLibArgs="${cLibArgs} --install"
1818
fi
1919

20+
2021
bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-c/main/download.sh) ${cLibArgs} ${cLibVersion}

objectbox/README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ class Person {
2424
2525
String firstName;
2626
String lastName;
27+
28+
Person({this.id = 0, required this.firstName, required this.lastName});
2729
}
2830
2931
// Note: in Flutter you also need to specify a directory, see examples.
3032
final store = Store(getObjectBoxModel());
3133
final box = store.box<Person>();
3234
33-
var person = Person()
34-
..firstName = "Joe"
35-
..lastName = "Green";
35+
var person = Person(firstName: 'Joe', lastName: 'Green');
3636
3737
final id = box.put(person); // Create
3838
39-
person = box.get(id); // Read
39+
person = box.get(id)!; // Read
4040
4141
person.lastName = "Black";
4242
box.put(person); // Update
@@ -87,6 +87,12 @@ dev_dependencies:
8787
```shell script
8888
bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-dart/main/install.sh)
8989
```
90+
91+
To install ObjectBox Sync variant of the native library, pass `--sync` argument to the script:
92+
93+
```shell script
94+
bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-dart/main/install.sh) --sync
95+
```
9096

9197
* macOS: dart might later complain that it cannot find the `libobjectbox.dylib`. You probably have to unsign the `dart`
9298
binary - see [this dart issue](https://github.com/dart-lang/sdk/issues/38314#issuecomment-534102841) for details.

objectbox/example/README.md

+27-28
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ import 'package:objectbox/objectbox.dart';
1717
1818
@Entity()
1919
class Note {
20-
// Each "Entity" needs a unique integer ID property.
21-
// Add `@Id()` annotation if its name isn't "id" (case insensitive).
22-
int id;
20+
// Each "Entity" needs a unique integer ID property.
21+
// Add `@Id()` annotation if its name isn't "id" (case insensitive).
22+
int id = 0;
2323
24-
String text;
24+
String? text;
2525
26-
DateTime date;
26+
DateTime date;
2727
28-
@Transient() // Make this field ignored, not stored in the database.
29-
int notPersisted;
28+
@Transient() // Make this field ignored, not stored in the database.
29+
int? notPersisted;
3030
31-
// An empty default constructor is needed but you can use optional args.
32-
Note({this.text});
31+
// An empty default constructor is needed but you can use optional args.
32+
Note({this.text, DateTime? date}) : date = date ?? DateTime.now();
3333
34-
// Note: just for logs in the examples below(), not needed by ObjectBox.
35-
toString() => 'Note{id: $id, text: $text}';
34+
// Note: just for logs in the examples below(), not needed by ObjectBox.
35+
toString() => 'Note{id: $id, text: $text}';
3636
}
3737
```
3838

@@ -121,7 +121,7 @@ final box = store.box<Note>();
121121
final note = Note(text: 'Hello'); // note: note.id is null
122122
final id = box.put(note); // note: sets note.id and also returns it
123123
print('new note got id=${id}, which is the same as note.id=${note.id}');
124-
print('refetched note: ${box.get(id)}');
124+
print('re-read note: ${box.get(id)}');
125125
```
126126

127127
Queries
@@ -175,7 +175,7 @@ Use "Property Queries" If you're interested only in a single property from an En
175175
You can access a list of property values across matching objects, or an aggregation.
176176

177177
```dart
178-
final query = box.query(Note_.date > 0).build()
178+
final query = box.query(Note_.date > 0).build();
179179
180180
// Use distinct or caseSensitive to refine results.
181181
final textQuery = query.stringProperty(Note_.text)
@@ -213,9 +213,8 @@ final sub1 = queryStream.listen((query) {
213213
214214
sub1.cancel();
215215
216-
final stream = query.findStream(limit:5);
217-
final sub2 = stream.listen((list) {
218-
// ...
216+
final sub2 = queryStream.listen((query) {
217+
print(query.find());
219218
});
220219
221220
// clean up
@@ -245,21 +244,21 @@ Have a look at the following example how a shop database could look like.
245244
```dart
246245
@Entity()
247246
class Customer {
248-
int id;
249-
String name;
247+
int id = 0;
248+
String? name;
250249
}
251250
252251
@Entity()
253252
class Order {
254-
int id;
253+
int id = 0;
255254
256255
final customer = ToOne<Customer>();
257256
final items = ToMany<Item>();
258257
}
259258
260259
@Entity()
261260
class Item {
262-
int id;
261+
int id = 0;
263262
}
264263
```
265264

@@ -291,24 +290,24 @@ Example: Two `Order` objects point to the same `Customer` using a `ToOne`. The b
291290
```dart
292291
@Entity()
293292
class Customer {
294-
int id;
295-
String name;
293+
int id = 0;
294+
String? name;
296295
297296
@Backlink()
298297
final orders = ToMany<Order>();
299298
}
300299
301300
@Entity()
302301
class Order {
303-
int id;
302+
int id = 0;
304303
305304
final customer = ToOne<Customer>();
306305
final items = ToMany<Item>();
307306
}
308307
309308
@Entity()
310309
class Item {
311-
int id;
310+
int id = 0;
312311
}
313312
```
314313

@@ -324,21 +323,21 @@ such a backlink to `Item`, so that we can access all `Order`s where this `Item`
324323
```dart
325324
@Entity()
326325
class Customer {
327-
int id;
328-
String name;
326+
int id = 0;
327+
String? name;
329328
}
330329
331330
@Entity()
332331
class Order {
333-
int id;
332+
int id = 0;
334333
335334
final customer = ToOne<Customer>();
336335
final items = ToMany<Item>();
337336
}
338337
339338
@Entity()
340339
class Item {
341-
int id;
340+
int id = 0;
342341
343342
@Backlink()
344343
final orders = ToMany<Order>();

objectbox/test/box_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void main() {
3939

4040
test('.put() returns a valid id', () {
4141
final object = TestEntity(tString: 'Hello');
42-
expect(object.id ?? 0, isZero);
42+
expect(object.id, isZero);
4343
int putId = box.put(object);
4444
expect(putId, greaterThan(0));
4545
expect(object.id, equals(putId)); // ID on the object is updated
@@ -458,7 +458,7 @@ void main() {
458458
test('assignable IDs', () {
459459
// TestEntity.id IS NOT assignable so this must fail
460460
expect(
461-
() => box.put(TestEntity(id: 1)),
461+
() => box.put(TestEntity()..id = 1),
462462
throwsA(predicate((ObjectBoxException e) => e
463463
.toString()
464464
.contains('ID is higher or equal to internal ID sequence'))));

objectbox/test/entity.dart

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestingUnknownAnnotation {
1313
@Sync()
1414
class TestEntity {
1515
@TestingUnknownAnnotation()
16-
int? id;
16+
int id = 0;
1717

1818
// implicitly determined types
1919
String? tString;
@@ -67,8 +67,7 @@ class TestEntity {
6767
Uint8List? tUint8List;
6868

6969
TestEntity(
70-
{this.id,
71-
this.tString,
70+
{this.tString,
7271
this.tLong,
7372
this.tDouble,
7473
this.tBool,
@@ -101,7 +100,7 @@ class TestEntity {
101100
tUint8List = Uint8List.fromList([7, 8, 9]);
102101
}
103102

104-
TestEntity.ignoredExcept(this.tInt) {
103+
TestEntity.ignoredExcept(this.tInt) : tString = '' {
105104
omit = -1;
106105
disregard = 1;
107106
}
@@ -136,7 +135,7 @@ class TestEntity {
136135
this.uShort,
137136
this.uByte,
138137
this.uChar,
139-
});
138+
}): tString = '';
140139

141140
@Property(type: PropertyType.byte)
142141
@Index()
@@ -168,7 +167,7 @@ class TestEntity {
168167
this.iShort,
169168
this.iByte,
170169
this.iChar,
171-
});
170+
}): tString = '';
172171

173172
final relA = ToOne<RelatedEntityA>();
174173
final relB = ToOne<RelatedEntityB>();

0 commit comments

Comments
 (0)