Skip to content

Commit

Permalink
Update Flutter SDK docs with nested collections
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Mar 15, 2024
1 parent 4adc566 commit 29b14bf
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 18 deletions.
35 changes: 31 additions & 4 deletions source/sdk/flutter/crud/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,37 @@ relationship. The following examples use the models defined on the

.. _flutter-query-list-realm-objects:

Query Lists
~~~~~~~~~~~
Query Collections
~~~~~~~~~~~~~~~~~

.. versionchanged:: v2.0.0
Query nested collections (``RealmList`` or ``RealmMap``) of mixed data

You can query collections of :flutter-sdk:`RealmObjects <realm/RealmObjectBase-mixin.html>`
or primitives. This includes:

- ``RealmList``
- ``RealmMap``
- ``RealmSet``

In Flutter SDK v2.0.0 and later, you can also query nested collections of mixed ``RealmValue`` data

Query RealmLists
````````````````

You can query any list of :flutter-sdk:`RealmObjects <realm/RealmObjectBase-mixin.html>`
or primitives.
You can query any list of

.. literalinclude:: /examples/generated/flutter/read_write_data_test.snippet.query-realm-list.dart
:language: dart

Query RealmMaps
````````````````

.. _flutter-query-set-realm-objects:

Query RealmSets
````````````````

.. _flutter-as-results:

Convert Lists or Sets to Results
Expand Down Expand Up @@ -241,6 +263,11 @@ You can use iterable arguments in your filter. For example:
.. literalinclude:: /examples/generated/flutter/read_write_data_test.snippet.filter-iterable.dart
:language: dart

Filter Nested Collections

Filter Inverse Relationships
````````````````````````````

You can also filter by inverse relationships using the
``@links.<Type>.<Property>`` syntax. For example, a filter can match
a ``Task`` object based on properties of the ``User``
Expand Down
61 changes: 47 additions & 14 deletions source/sdk/flutter/realm-database/model-data/data-types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ Data Types - Flutter SDK
========================

.. meta::
:keywords: code example, flutter
:keywords: code example
:description: Learn about the data types Atlas Device SDK for Flutter supports and how to use them.

.. facet::
:name: genre
:values: tutorial

.. contents:: On this page
:local:
:backlinks: none
Expand Down Expand Up @@ -151,9 +155,9 @@ When defining a RealmSet in a schema:
- A set of primitive types can be defined as either nullable or non-nullable.
For example, both ``Set<int>`` and ``Set<int?>`` are valid in a Realm schema.
- A set of ``RealmObject`` and ``RealmValue`` types can only be non-nullable.
For example ``Set<RealmValue>`` **is valid** and ``Set<RealmValue?>`` **is not valid**.
- You **cannot** define default values when defining a set in a schema.
For example, ``Set mySet = {0,1,2}`` **is not valid**.
For example ``Set<RealmValue>`` is valid, but ``Set<RealmValue?>`` *is not* valid.
- You *cannot* define default values when defining a set in a schema.
For example, ``Set mySet = {0,1,2}`` *is not* valid.

.. literalinclude:: /examples/generated/flutter/data_types_test.snippet.realm-set-model.dart
:language: dart
Expand Down Expand Up @@ -238,10 +242,10 @@ Collections are Live
Like :ref:`live objects <flutter-live-object>`, Realm collections
are *usually* live:

- Live **results collections** always reflect the current results of the associated query.
- Live **lists** of ``RealmObjects`` always reflect the current state of the relationship on the realm instance.
- **Live results collections** always reflect the current results of the associated query.
- **Live lists** of ``RealmObjects`` always reflect the current state of the relationship on the realm instance.

There are two cases, however, when a collection is **not** live:
There are two cases, however, when a collection is *not* live:

- The collection is unmanaged: a ``RealmList`` property of a Realm object
that has not been added to a realm yet or that has been copied from a
Expand Down Expand Up @@ -339,18 +343,24 @@ following things are true when using ``compareTo()``:
RealmValue
~~~~~~~~~~

.. versionchanged:: 2.0.0
``RealmValue`` can be a ``List`` or ``Map`` of ``RealmValue`` type.
``RealmValueType`` enum and ``RealmValue.value.runtimeType`` added.
``RealmValue.binary`` replaces ``RealmValue.uint8List``.

The `RealmValue <https://pub.dev/documentation/realm_common/latest/realm_common/RealmValue-class.html>`__
data type is a mixed data type that can represent any other valid Realm data type except a collection.
You can create collections of type ``RealmValue``, but a ``RealmValue`` itself
cannot be a collection. ``RealmValue`` is indexable, but cannot be a primary key.
data type is a mixed data type that can represent any other valid Realm
data type. In Flutter SDK v2.0.0 and later, ``RealmValue`` can represent a List or Map collection (but *not* a Set) of mixed type.

.. note::
``RealmValue`` is indexable, but cannot be a primary key. You can also create collections of type ``RealmValue``.

.. note:: ``RealmValue`` Not Nullable But Can Contain Null Values

When defining your Realm object schema, you cannot create a nullable ``RealmValue``.
However, if you want a ``RealmValue`` property to contain a null value,
you can use the special ``RealmValue.nullValue()`` property.

To define a property as ``RealmValue``, set its type in your Realm object model.
To define a property as ``RealmValue``, set its type in your object model.

.. literalinclude:: /examples/generated/flutter/data_types_test.snippet.realm-value-model.dart
:language: dart
Expand All @@ -360,7 +370,30 @@ To add a ``RealmValue`` to a Realm object, call ``RealmValue.from()`` on the dat
.. literalinclude:: /examples/generated/flutter/data_types_test.snippet.realm-value-from.dart
:language: dart

Access the type of data with ``RealmValue.type`` and the value with ``RealmValue.value``.
In Flutter SDK v2.0.0 and later, you access the type of data with the ``RealmValueType`` enum and the value with ``RealmValue.value``. You can also access the runtime type of the value with ``RealmValue.value.runtimeType``.

In earlier SDK versions, you access the type of data with ``RealmValue.type``.

.. literalinclude:: /examples/generated/flutter/data_types_test.snippet.realm-value-type-value.dart
:language: dart

.. _flutter-nested-collections-realm-value:

Nested Collections of RealmValue
````````````````````````````````

.. versionadded:: v2.0.0

Starting in Flutter SDK version 2.0.0, a ``RealmValue`` data type can contain nested collections (a list or map) of ``RealmValue`` type:

- ``RealmValue<List<RealmValue>>``
- ``RealmValue<RealmMap<String, RealmValue>>``

It *cannot* contain a set of mixed type.

You can use nested collections of mixed type to define unstructured or variable data. For more information, refer to :ref:`<flutter-model-unstructured-data>`.

You can work with nested collections like a normal collection of mixed type.

.. literalinclude:: /examples/generated/flutter/data_types_test.snippet.realm-value-type-value.dart
:language: dart
Expand All @@ -371,7 +404,7 @@ Uint8List
`Uint8List <https://api.dart.dev/stable/3.0.5/dart-typed_data/Uint8List-class.html>`__
is a binary data type from `dart:typed_data
<https://api.dart.dev/stable/3.0.5/dart-typed_data/dart-typed_data-library.html>`__.
You can use this binary data type in object models and property values.
You can use this data type in object models and property values.

To define a property as ``Uint8List``, you must first import ``dart:typed_data``.
Then, set the object's type as ``Uint8List`` in your :ref:`object model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Define a Realm Object Schema - Flutter SDK

.. meta::
:keywords: code example, android, ios, data modeling
:description: Define the properties and relationships of database objects within your data model.

.. facet::
:name: genre
Expand Down Expand Up @@ -233,6 +234,27 @@ If you're using Atlas Device Sync, the name that you specify in the
:language: dart
:emphasize-lines: 15-16

.. _flutter-model-unstructured-data:

Model Unstructured Data
-----------------------

.. versionadded:: 2.0.0

Starting in Flutter SDK version 2.0.0, you can leverage nested collections within a :ref:`RealmValue (Mixed) <flutter-realm-value>` type to define data that doesn't otherwise conform to an expected schema.
This could include data with variable structures or data whose shape or type is not known at runtime. For example, user-created objects, event logs, or survey response data that are collected and stored in a variety of JSON formats. Collections of mixed data could also be a good temporary option when you're in the early development stages and still determining the structure of your app's data.

To model unstructured data in your schema, define the appropriate properties as ``RealmValue`` types that contain either a List or Map of ``RealmValue`` type:
``RealmValue<List<RealmValue>>`` or ``RealmValue<RealmMap<String, RealmValue>>``.
The SDK does *not* support a Mixed data type containing a Set collection.

.. tip::

Use a Map of Mixed data types when the type is unknown, but each value will have a unique identifier.
Use a List of Mixed data types when the type is unknown, but the order of objects is meaningful.

.. define a generic JSON structure to use across all SDK examples and add to doc plan/tickets

.. _flutter-generate-realm-object:

Generate the RealmObject
Expand Down

0 comments on commit 29b14bf

Please sign in to comment.