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]: Invalid argument(s): Element (FieldElementImpl) $Match? match is not defined in this library (When using @Backlink) #1015

Closed
geosebas opened this issue Nov 5, 2022 · 2 comments · Fixed by #1016

Comments

@geosebas
Copy link

geosebas commented Nov 5, 2022

What happened?

Hi !

The bug here is that you cannot use a backlink if not in the same file/library.
That may be more related to dart that realm, somethink like Symbol cannot look up for properties not in the same file/library ? I'm not a dart/flutter specialist, so I prefer to fill a bug here to report the issue to you.

And on a related note, you cannot use List as a type, just Iterable. Imho, it's something to improve, at least to keep consistency with other relation that use List.

Repro steps

https://github.com/geosebas/flutter_realm_backlink

Version

0.7.0rc

What Realm SDK flavor are you using?

MongoDB Atlas (i.e. Sync, auth, functions)

What type of application is this?

Flutter Application

Client OS and version

Generation realm

Code snippets

No response

Stacktrace of the exception/crash you're getting

Invalid argument(s): Element (FieldElementImpl) $CourtSlot? slots3 is not defined in this library.
#0      ResolvedLibraryResultImpl.getElementDeclaration.<anonymous closure> (package:analyzer/src/dart/analysis/results.dart:251:9)
#1      ListMixin.firstWhere (dart:collection/list.dart:166:38)
#2      ResolvedLibraryResultImpl.getElementDeclaration (package:analyzer/src/dart/analysis/results.dart:247:28)
#3      getDeclarationFromElement (package:realm_generator/src/element.dart:38:34)
#4      FieldElementEx.declarationAstNode (package:realm_generator/src/field_element_ex.dart:40:46)
#5      ElementEx.declarationAstNode (package:realm_generator/src/element.dart:72:43)
#6      ElementEx._annotationsInfoOfExact (package:realm_generator/src/element.dart:79:18)
#7      _SyncStarIterator.moveNext (dart:async-patch/async_patch.dart:710:21)
#8      new _GrowableList._ofOther (dart:core-patch/growable_array.dart:202:26)
#9      new _GrowableList.of (dart:core-patch/growable_array.dart:152:26)
#10     new List.of (dart:core-patch/array_patch.dart:51:28)
#11     Iterable.toList (dart:core/iterable.dart:470:12)
#12     ElementEx.annotationInfoOfExact (package:realm_generator/src/element.dart:89:58)
#13     FieldElementEx.realmInfo (package:realm_generator/src/field_element_ex.dart:250:44)
#14     _extension#0.realmInfo (package:realm_generator/src/class_element_ex.dart:37:22)
#15     _SyncStarIterator.moveNext (dart:async-patch/async_patch.dart:710:21)
#16     new _GrowableList._ofOther (dart:core-patch/growable_array.dart:202:26)
#17     new _GrowableList.of (dart:core-patch/growable_array.dart:152:26)
#18     new List.of (dart:core-patch/array_patch.dart:51:28)
#19     Iterable.toList (dart:core/iterable.dart:470:12)
#20     ClassElementEx.realmInfo (package:realm_generator/src/class_element_ex.dart:156:45)
#21     _extension#0.realmInfo.<anonymous closure> (package:realm_generator/src/realm_object_generator.dart:71:58)
#22     MappedIterator.moveNext (dart:_internal/iterable.dart:391:20)
#23     WhereIterator.moveNext (dart:_internal/iterable.dart:438:22)
#24     CastIterator.moveNext (dart:_internal/cast.dart:61:30)
#25     ExpandIterator.moveNext (dart:_internal/iterable.dart:477:21)
#26     Iterable.join (dart:core/iterable.dart:423:19)
#27     RealmObjectGenerator.generate.<anonymous closure>.<anonymous closure> (package:realm_generator/src/realm_object_generator.dart:61:69)
#28     _rootRun (dart:async/zone.dart:1391:13)
#29     _CustomZone.run (dart:async/zone.dart:1293:19)
#30     _runZoned (dart:async/zone.dart:1829:10)
#31     runZonedGuarded (dart:async/zone.dart:1817:12)
#32     scopeSession (package:realm_generator/src/session.dart:44:16)
#33     RealmObjectGenerator.generate.<anonymous closure> (package:realm_generator/src/realm_object_generator.dart:59:16)
<asynchronous suspension>
#34     measure.<anonymous closure> (package:realm_generator/src/measure.dart:50:18)
<asynchronous suspension>
#35     measure (package:realm_generator/src/measure.dart:47:7)
<asynchronous suspension>
#36     RealmObjectGenerator.generate (package:realm_generator/src/realm_object_generator.dart:56:12)
<asynchronous suspension>
#37     _generate (package:source_gen/src/builder.dart:352:23)
<asynchronous suspension>
#38     Stream.toList.<anonymous closure> (dart:async/stream.dart:1349:9)
<asynchronous suspension>


in: package:flutter_realm_backlink/court_slot.dart:18:25
    ╷
6   │ @RealmModel()
7   │ class $CourtSlot {
    │       ━━━━━━━━━━ in realm model for 'CourtSlot'
... │
16  │   /// Not working not in the same file/library
17  │   @Backlink(#slots3)
18  │   late Iterable<$Court> courts;
    │                         ^^^^^^ !
    ╵
Unexpected error. Please open an issue on: https://github.com/realm/realm-dart

### Relevant log output
_No response_

@nielsenko
Copy link
Contributor

Hi @geosebas

Thanks for reporting this!

Regarding cross file backlinks, then this is indeed a bug in the generator, and I have pushed a PR to fix it.

Regarding your suggestion to use List<T> over Iterable<T>. A field marked as a Backlink has a number of constraints:

  1. You cannot modify them directly. You change the backlinks by changing the link pointing to the object.
  2. The order of elements is mostly outside your control, and not something to depend on.

The type hierarchy in Dart regarding collections does not allow for a lot of static differentiation, so we chose Iterable over List. There was some discussion about using UnmodifiableListView but it inherits List.

Anyway, this is just for the models. In the generated class the backlink property is RealmResults<T>. This means you can still index into them, get their length efficiently, and subscribe to changes.

@geosebas
Copy link
Author

geosebas commented Nov 6, 2022

Hi @nielsenko

Thanks for the explanation and the fix !

Have a nice day !

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants