Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: showcase the bug with linkMany relation #402

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 71 additions & 41 deletions objectbox/example/flutter/objectbox_demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,72 @@ class _MyHomePageState extends State<MyHomePage> {
}

GestureDetector Function(BuildContext, int) _itemBuilder(List<Note> notes) =>
(BuildContext context, int index) => GestureDetector(
onTap: () => objectbox.noteBox.remove(notes[index].id),
child: Row(
children: <Widget>[
Expanded(
child: Container(
decoration: const BoxDecoration(
border:
Border(bottom: BorderSide(color: Colors.black12))),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 18.0, horizontal: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
notes[index].text,
style: const TextStyle(
fontSize: 15.0,
),
// Provide a Key for the integration test
key: Key('list_item_$index'),
(BuildContext context, int index) {
final note = notes[index];
return GestureDetector(
onTap: () => objectbox.noteBox.remove(note.id),
child: Row(
children: <Widget>[
Expanded(
child: Container(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.black12),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 18.0, horizontal: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
note.text,
style: const TextStyle(
fontSize: 15.0,
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child: Text(
'Added on ${notes[index].dateFormat}',
style: const TextStyle(
fontSize: 12.0,
// Provide a Key for the integration test
key: Key('list_item_$index'),
),
Wrap(
children: [
for (final att in note.attachment)
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Text(
'A: ${att.id}',
style: TextStyle(fontSize: 10),
),
),
],
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child: Text(
'Added on ${notes[index].dateFormat}',
style: const TextStyle(
fontSize: 12.0,
),
),
],
),
),
],
),
),
),
],
),
);
),
IconButton(
onPressed: () {
note.attachment.add(
Attachment('Hello'),
);
objectbox.noteBox.put(note);
},
icon: const Icon(Icons.attach_file),
)
],
),
);
};

@override
Widget build(BuildContext context) => Scaffold(
Expand All @@ -125,7 +151,8 @@ class _MyHomePageState extends State<MyHomePage> {
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
decoration: const InputDecoration(
hintText: 'Enter a new note'),
hintText: 'Enter a new note',
),
controller: _noteInputController,
onSubmitted: (value) => _addNote(),
// Provide a Key for the integration test
Expand All @@ -152,13 +179,16 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
Expanded(
child: StreamBuilder<List<Note>>(
stream: _listController.stream,
builder: (context, snapshot) => ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.symmetric(horizontal: 20.0),
itemCount: snapshot.hasData ? snapshot.data!.length : 0,
itemBuilder: _itemBuilder(snapshot.data ?? []))))
child: StreamBuilder<List<Note>>(
stream: _listController.stream,
builder: (context, snapshot) => ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.symmetric(horizontal: 20.0),
itemCount: snapshot.hasData ? snapshot.data!.length : 0,
itemBuilder: _itemBuilder(snapshot.data ?? []),
),
),
)
]),
// We need a separate submit button because flutter_driver integration
// test doesn't support submitting a TextField using "enter" key.
Expand Down
26 changes: 22 additions & 4 deletions objectbox/example/flutter/objectbox_demo/lib/model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:intl/intl.dart';
import 'package:objectbox/objectbox.dart';

Expand All @@ -6,7 +7,10 @@ import 'objectbox.g.dart';
// ignore_for_file: public_member_api_docs

@Entity()
class Note {
class Note extends Equatable {
Note(this.text, {this.id = 0, this.comment, DateTime? date})
: date = date ?? DateTime.now();

int id;

String text;
Expand All @@ -15,8 +19,22 @@ class Note {
/// Note: Stored in milliseconds without time zone info.
DateTime date;

Note(this.text, {this.id = 0, this.comment, DateTime? date})
: date = date ?? DateTime.now();

String get dateFormat => DateFormat('dd.MM.yyyy hh:mm:ss').format(date);

ToMany<Attachment> attachment = ToMany<Attachment>();

@override
List<Object?> get props => [id, text, comment, date];
}

@Entity()
class Attachment extends Equatable {
Attachment(this.content);

int id = 0;

final String content;

@override
List<Object?> get props => [id, content];
}
29 changes: 27 additions & 2 deletions objectbox/example/flutter/objectbox_demo/lib/objectbox-model.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,37 @@
"type": 10
}
],
"relations": [
{
"id": "1:7144087601680027171",
"name": "attachment",
"targetId": "2:332880768092001438"
}
]
},
{
"id": "2:332880768092001438",
"lastPropertyId": "2:3461296757897168890",
"name": "Attachment",
"properties": [
{
"id": "1:5876356419799926351",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:3461296757897168890",
"name": "content",
"type": 9
}
],
"relations": []
}
],
"lastEntityId": "1:2802681814019499133",
"lastEntityId": "2:332880768092001438",
"lastIndexId": "0:0",
"lastRelationId": "0:0",
"lastRelationId": "1:7144087601680027171",
"lastSequenceId": "0:0",
"modelVersion": 5,
"modelVersionParserMinimum": 5,
Expand Down
4 changes: 3 additions & 1 deletion objectbox/example/flutter/objectbox_demo/lib/objectbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class ObjectBox {
noteBox = Box<Note>(store);

final qBuilder = noteBox.query()
..order(Note_.date, flags: Order.descending);
..order(Note_.date, flags: Order.descending)
// Comment to see it working
..linkMany(Note_.attachment);
queryStream = qBuilder.watch(triggerImmediately: true);

// Add some demo data if the box is empty.
Expand Down
1 change: 1 addition & 0 deletions objectbox/example/flutter/objectbox_demo/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
intl: any
objectbox: ^1.4.1
objectbox_flutter_libs: any
equatable: ^2.0.3

dev_dependencies:
objectbox_generator: any
Expand Down