How to fix Error: Cannot write to class Task when no flexible sync subscription has been created

Hi, I am doing a simple example and am getting this error when adding a task to the database. I have fully configured on Device Sync.

Thank you for your time.




Hi @Minh_Quang_H_Vu,

As the error in the attached screenshot alludes to, before the realm.write<Tasks>(...) you need to add a subscription on the Tasks table. Please see the docs for more information.

Jonathan

I’m really sorry but after seeing a docs I still don’t know what to do.

Can you share the realm model for Task?

import 'package:realm/realm.dart';

part 'task.g.dart';

@RealmModel()
class _Tasks {
  @PrimaryKey()
  @MapTo("_id")
  late ObjectId id;

  late String title;
  late String date;
  late String userId;
}

Ah ok. I didn’t notice at first that you had an asymmetric table setup on Tasks (sorry about that!). Asymmetric / data ingest sync is currently unsupported in the Flutter SDK. Does your use case require making the Tasks table asymmetric?

I do not know. Now what should I do to fix it. This code I was able to run before and query the data and display it, but now it can’t.

So data ingest sync is designed and optimized for insert-only workloads (syncing from the device to MongoDB, but not the other way around).

Given that you are querying on the data being synced (Tasks), it sounds like data ingest sync is not suited for your use case (let me know if this isn’t the case though, I’m admittedly not a Flutter expert).

I would recommend removing Tasks as a data ingest collection by doing the following:

  1. Terminate sync
  2. Remove Tasks as a data ingest collection. This can be achieved in the sync config UI by clicking the x marker next to Tasks under Collections from your schema in the Data Ingest section. Then, re-enable sync.

After re-enablement, Tasks will be synced as a normal table/collection. For context, the reasoning for the termination and re-enablement of sync is because removing a data ingest collection is a destructive schema change (read here for more information regarding the consequences of these types of changes).

Let me know if that helps,
Jonathan

I really don’t understand. Now it works even though I didn’t edit anything. But after adding 4 tasks with the “add” button and manually deleting them directly on the collections, they are not synchronized


1 Like

That is exactly the nature of data ingest sync. Changes will be synchronized from the device to MongoDB Atlas, but not the other way around (I would recommend reading the link I sent above). In your situation, the deletions in Atlas were not propagated back down to the client. I’m actually surprised that synchronization is working at all because as I mentioned above, data ingest is currently unsupported in the Flutter SDK.

1 Like

I ran as expected. I really thank you for your enthusiastic help.

1 Like