Flutter: ZefyrEditor does not show updated data fetched from a Isar db even if it saves data correctly

35 views Asked by At

I'm using ZefyrEditor for the WYSIWYG editor for description from an Isar db. It saves the text correctly when edited but when opened next time, it shows the old text even if the db has the updated text.

The functions related to the Zefyr Editor

  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _tagController = TextEditingController();

  ZefyrController _zefyrController = ZefyrController();
  bool isDone = false;
  bool isTrash = false;

  List<String> tagsValue = [];

  DateTime? reminderValue;

  void _getdata() async {
    final updateTask = await context
        .read<TaskListProvider>()
        .isarCollection!
        .get(widget.todoId);

    setState(() {
      isDone = updateTask!.doneStatus;
      _titleController.text = updateTask.title!;
      _zefyrController = ZefyrController(
          NotusDocument.fromJson(jsonDecode(updateTask.description!)));
      isTrash = updateTask.trash;
      _selectedPriority = updateTask.priority ?? Priority.low;
    });
    print(_zefyrController.document.toJson());
  }

  Future<bool> checkForChanges() async {
    final updateTask = await context
        .read<TaskListProvider>()
        .isarCollection!
        .get(widget.todoId);

    return (_titleController.text == updateTask?.title) &&
        (jsonEncode(_zefyrController.document.toJson()) ==
            updateTask?.description);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void initState() {
    super.initState();
    //Tried with WidgetsBinding and without SchedulerBinding too
    scd.SchedulerBinding.instance.addPostFrameCallback((_) {
      _getdata();
    });
    //_getdata();
  }

  @override
  void dispose() {
    super.dispose();
    _titleController.dispose();
    _zefyrController.dispose();
  }

...
      floatingActionButton: FloatingActionButton(
          onPressed: () async {
            if (_titleController.text.isEmpty) {
              ScaffoldMessenger.of(context)
                ..removeCurrentSnackBar()
                ..showSnackBar(const SnackBar(
                  content: Text("Title cannot be blank"),
                  behavior: SnackBarBehavior.floating,
                ));
            } else {
              taskListProvider.updateTodoTitle(
                  widget.todoId, _titleController.text);
              taskListProvider.updateTodoDescription(widget.todoId,
                  jsonEncode(_zefyrController.document.toJson()));
              print(jsonEncode(_zefyrController.document.toJson()));
              taskListProvider.markTaskDone(widget.todoId, isDone);
              taskListProvider.updatePriority(widget.todoId, _selectedPriority);
              _titleController.clear();
              Navigator.of(context).pop();
            }
          },
          child: const Icon(Icons.done_rounded),

Provider functions used to fetch data from isar db

class TaskListProvider with ChangeNotifier {
  TaskListProvider() {
    init();
  }

  IsarCollection<TaskData>? _isarCollection;
  IsarCollection<TaskData>? get isarCollection => _isarCollection;

  List<TaskData> _taskList = [];
  List<TaskData> get taskList => _taskList;

  Isar? isar;

  void init() async {
    final dir = await getApplicationDocumentsDirectory();
    isar = await Isar.open([TaskDataSchema], directory: dir.path);

    _taskList = await isar!.taskDatas
        .where()
        .filter()
        .archiveEqualTo(false)
        .trashEqualTo(false)
        .findAll();
    _isarCollection = isar!.taskDatas;
    notifyListeners();
  }
...

  void updateTodoDescription(int id, String desc) async {
    final updateTask = await isar!.taskDatas.get(id);
    updateTask?.description = desc;
    updateTask?.dateModified = DateTime.now();
    isar?.writeTxn(() async {
      isar!.taskDatas.put(updateTask!);
    });
    await updateListinProvider();
    notifyListeners();
  }
  
  Future<void> updateListinProvider() async {
    _taskList = await isar!.taskDatas
        .where()
        .filter()
        .archiveEqualTo(false)
        .trashEqualTo(false)
        .findAll();
    _isarCollection = isar!.taskDatas;
  }
}

Exceptions: Editor showing updated description text.

Reality: It doesn't

I tried setting a blank document with setState before running _getData()

0

There are 0 answers