flutterdata / flutter_data

Seamlessly manage persistent data in your Flutter apps
MIT License
410 stars 31 forks source link

How to query local data while offline #253

Closed greenking19 closed 10 months ago

greenking19 commented 10 months ago

I have two tables, one is User and the other is Todo Due to design reasons, they are not related. Due to design reasons, they are not associated, but the todo table has a userid field. User Table

@JsonSerializable()
@DataRepository([JSONServerAdapter], internalType: 'user')
class User extends DataModel<User> {
  @override
  final String id;
  final String nickname;

  User({
    required this.id,
    required this.nickname,
  });
}

Todo Table

@JsonSerializable()
@DataRepository([JSONServerAdapter], internalType: 'to-do')
class Todo extends DataModel<Todo> {
  @override
  final String? id;
  final String title;
  final bool completed;
  final String? userId;

  Todo({
    this.id,
    required this.title,
    this.completed = false,
    this.userId,
  });

  Todo toggleCompleted() {
    return Todo(
      id: id,
      title: title,
      completed: !completed,
      userId: userId,
    ).withKeyOf(this);
  }
}

How to query local data when offline? Similar to MySQL

SELECT todo WHERE userId="xxx"

Another question is, I would like to know which data was created offline or created but not uploaded to the server. How do I need to search?

Sometimes I may need to clear all local data, can this be done? This is the issue I encountered while using Flutterdata. I hope to get your help.🙋 thank you!👨🏻‍💻

frank06 commented 10 months ago

At the moment it's not possible to query, but I'm working on moving the backend from Hive to a database where queries will be possible.

For things created offline AND that were attempted to sync with a server but failed, try inspecting repository.offlineOperations. If you did not attempt to save they will not appear there

(Btw there is no need to use internalType in the annotation)

ad6025b commented 7 months ago

@frank06 can you please show me example code of how to access repository.offlineOperations ???

frank06 commented 7 months ago

@alexdoan102 ref.users.offlineOperations