4
4
Define a Realm Object Schema - Flutter SDK
5
5
==========================================
6
6
7
- .. meta::
7
+ .. meta::
8
8
:keywords: code example, android, ios, data modeling
9
9
:description: Define the properties and relationships of database objects within your data model.
10
10
@@ -62,8 +62,8 @@ Create Model
62
62
.. step:: Create Generated File Part Directive
63
63
64
64
.. versionchanged:: v2.0.0
65
- Generated files are named ``.realm.dart`` instead of ``.g.dart``
66
-
65
+ Generated files are named ``.realm.dart`` instead of ``.g.dart``
66
+
67
67
Add a part directive to include the ``RealmObject`` file that you generate in step 4
68
68
in the same package as the file you're currently working on.
69
69
@@ -96,13 +96,13 @@ Create Model
96
96
.. literalinclude:: /examples/generated/flutter/schemas.snippet.create-realm-model.dart
97
97
:language: dart
98
98
:caption: schemas.dart
99
-
99
+
100
100
.. include:: /includes/note-class-char-limit.rst
101
101
102
102
.. step:: Generate RealmObject
103
103
104
104
.. versionchanged:: v2.0.0
105
- Generated files are named ``.realm.dart`` instead of ``.g.dart``
105
+ Generated files are named ``.realm.dart`` instead of ``.g.dart``
106
106
107
107
Generate the ``RealmObject``, which you'll use in your application:
108
108
@@ -211,19 +211,19 @@ Map a Model or Class to a Different Name
211
211
----------------------------------------
212
212
213
213
You can use the `MapTo <https://pub.dev/documentation/realm_common/latest/realm_common/MapTo-class.html>`__
214
- annotation to map a Realm object model or property to a different stored
214
+ annotation to map a Realm object model or property to a different stored
215
215
name in Realm. This can be useful in the following scenarios. For example:
216
216
217
- - To make it easier to work across platforms where naming conventions
218
- differ. For example, if your Device Sync schema property names use snake
217
+ - To make it easier to work across platforms where naming conventions
218
+ differ. For example, if your Device Sync schema property names use snake
219
219
case, while your project uses camel case.
220
220
- To change a class or field name without forcing a migration.
221
221
- To support multiple model classes with the same name in different packages.
222
- - To use a class name that is longer than the 57-character limit enforced
222
+ - To use a class name that is longer than the 57-character limit enforced
223
223
by Realm.
224
224
225
- If you're using Atlas Device Sync, the name that you specify in the
226
- ``MapTo`` annotation is used as the persisted :ref:`App Services Schema
225
+ If you're using Atlas Device Sync, the name that you specify in the
226
+ ``MapTo`` annotation is used as the persisted :ref:`App Services Schema
227
227
<schemas>` name.
228
228
229
229
.. tabs::
@@ -244,40 +244,48 @@ If you're using Atlas Device Sync, the name that you specify in the
244
244
245
245
.. _flutter-model-unstructured-data:
246
246
247
- Model Unstructured Data
247
+ Model Unstructured Data
248
248
-----------------------
249
249
250
250
.. versionadded:: 2.0.0
251
251
252
- Starting in Flutter SDK version 2.0.0, you can
253
- :ref:`define nested collections
254
- of mixed data <flutter-nested-collections-realm-value>`
255
- within a ``RealmValue`` property.
256
-
257
- The ability to nest collections of mixed data enables you to define data
258
- that doesn't otherwise conform to an expected schema, including data with
259
- variable structure or data whose shape or type is not known at runtime.
260
- For example, you might have highly variable user-created objects, event logs,
261
- or survey response data that are collected and stored in a variety of JSON
262
- formats. This approach allows you to :ref:`react to changes <flutter-realm-list-change-listener>`
263
- in the nested data and to update specific elements, but it is less
264
- performant than using a structured schema or serializing JSON blobs
265
- into a single string property.
266
-
267
- To model unstructured data in your schema using collections of mixed type,
268
- define the appropriate properties in your schema as
269
- :ref:`RealmValue <flutter-realm-value>` types. You can then set these
270
- ``RealmValue`` properties as a :ref:`RealmList <flutter-realm-list>` or a
271
- :ref:`RealmMap <flutter-realm-map>` collection of ``RealmValue`` elements.
252
+ Starting in Flutter SDK version 2.0.0, you can store
253
+ :ref:`collections of mixed data <flutter-nested-collections-realm-value>`
254
+ within a ``RealmValue`` property. You can use this feature to model complex
255
+ data structures, such as JSON or MongoDB documents, without having to define a
256
+ strict data model.
257
+
258
+ **Unstructured data** is data that doesn't easily conform to an expected
259
+ schema, making it difficult or impractical to model to individual
260
+ data classes. For example, your app might have highly variable data or dynamic
261
+ data whose structure is unknown at runtime.
262
+
263
+ Storing collections in a mixed property offers flexibility without sacrificing
264
+ functionality, including performant synchronization when using Device Sync. And
265
+ you can work with them the same way you would a non-mixed
266
+ collection:
267
+
268
+ - You can nest mixed collections up to 100 levels.
269
+ - You can query on and :ref:`react to changes <flutter-realm-list-change-listener>`
270
+ on mixed collections.
271
+ - You can find and update individual mixed collection elements.
272
+
273
+ However, storing data in mixed collections is less performant than using a structured
274
+ schema or serializing JSON blobs into a single string property.
275
+
276
+ To model unstructured data in your app, define the appropriate properties in
277
+ your schema as :ref:`RealmValue <flutter-realm-value>` types. You can then set
278
+ these ``RealmValue`` properties as a :ref:`RealmList <flutter-realm-list>` or a
279
+ :ref:`RealmMap <flutter-realm-map>` collection of ``RealmValue`` elements.
272
280
Note that ``RealmValue`` *cannot* represent a ``RealmSet`` or an embedded object.
273
281
274
- For example, you might use a ``RealmValue`` that contains a map of mixed
275
- data when modeling a variable event log object:
282
+ For example, you might use a ``RealmValue`` that contains a map of mixed
283
+ data when modeling a variable event log object:
276
284
277
285
.. literalinclude:: /examples/generated/flutter/define_realm_model_test.snippet.unstructured-data-model.dart
278
286
:language: dart
279
287
:emphasize-lines: 10
280
- :caption: Data model
288
+ :caption: Data model
281
289
282
290
.. io-code-block::
283
291
:copyable: true
@@ -286,7 +294,7 @@ data when modeling a variable event log object:
286
294
.. input:: /examples/generated/flutter/define_realm_model_test.snippet.create-unstructured-data-example.dart
287
295
:language: dart
288
296
289
- .. output::
297
+ .. output::
290
298
:language: shell
291
299
292
300
Event Type: purchase
@@ -298,9 +306,9 @@ data when modeling a variable event log object:
298
306
items: RealmValue([RealmValue({id: RealmValue(1), name: RealmValue(Laptop), price: RealmValue(1200.0)}), RealmValue({id: RealmValue(2), name: RealmValue(Mouse), price: RealmValue(49.99)})])
299
307
total: RealmValue(1249.99)
300
308
301
- .. tip::
309
+ .. tip::
302
310
303
- - Use a map of mixed data types when the type is unknown but each value will have a unique identifier.
311
+ - Use a map of mixed data types when the type is unknown but each value will have a unique identifier.
304
312
- Use a list of mixed data types when the type is unknown but the order of objects is meaningful.
305
313
306
314
.. _flutter-generate-realm-object:
@@ -309,10 +317,10 @@ Generate the RealmObject
309
317
------------------------
310
318
311
319
.. versionchanged:: v2.0.0
312
- Generated files are named ``.realm.dart`` instead of ``.g.dart``
320
+ Generated files are named ``.realm.dart`` instead of ``.g.dart``
313
321
314
322
Once you've completed your Realm model, you must generate the
315
- :flutter-sdk:`RealmObject <realm/RealmObjectBase-mixin.html>` class to use
323
+ :flutter-sdk:`RealmObject <realm/RealmObjectBase-mixin.html>` class to use
316
324
it in your application.
317
325
318
326
Run the following command to generate ``RealmObjects``:
@@ -410,7 +418,7 @@ Asymmetric objects require Flexible Sync. To define an asymmetric object, pass
410
418
411
419
In Flutter SDK versions 1.5.0 and earlier, you cannot link from
412
420
``asymmetricObject`` types to ``RealmObjects``. In SDK versions 1.6.0 and
413
- later, ``asymmetricObject`` types can link to ``RealmObjects`` in
421
+ later, ``asymmetricObject`` types can link to ``RealmObjects`` in
414
422
addition to :ref:`embedded object types <flutter-embedded-objects>`.
415
423
416
424
.. note:: Attempting to Read Asymmetric Objects
@@ -419,5 +427,5 @@ addition to :ref:`embedded object types <flutter-embedded-objects>`.
419
427
object, you will get the following error: "Error: You can't query an
420
428
asymmetric class.".
421
429
422
- To learn more about Data Ingest, refer to :ref:`Stream Data to Atlas
430
+ To learn more about Data Ingest, refer to :ref:`Stream Data to Atlas
423
431
<flutter-stream-data-to-atlas>`.
0 commit comments