flutterdata / flutter_data

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

How to show state info in log? #166

Closed huang12zheng closed 2 years ago

huang12zheng commented 2 years ago

Today, I try to update dependencies in example https://github.com/flutterdata/flutter_data_todos/ ~~but find some problem after I do some update. one of them is console never show state change log. I check source, and then I think that it is relation to logLevel too low. So, how to set it in flutter?~~

I just forget git pull newest repo. So, stroke they

But, how to show the detail of todo? The output of Log is just

47:249 [save/todos#1@664a86] saved in local storage only

I want a more info like:

{"id":"xx","title":"xx","completed":false}
huang12zheng commented 2 years ago

where to change logLevel

final userProvider = StateNotifierProvider.autoDispose<DataStateNotifier<User?>,
    DataState<User?>>((ref) {
  ref.users.logLevel = 3;
  ref.todos.logLevel = 3;
  return ref.users.watchOneNotifier(1,
      params: {'_embed': 'todos'}, alsoWatch: (u) => {u.todos});
});
huang12zheng commented 2 years ago

By the way, in last version flutter_data_todos. If I quickly click todo to toggle completed, It would outcome wrong state through state log. Now, It look like that state work well So, I would ask why happen state wrong in last version(d92f20f760f2d36d0a57bb3c04376a23ee3968a7)?

[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":false}
[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":true} <<<<<<<<
[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":true} <<<<<<<<
[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":false}
[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":true}
[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":false} <<<<<<<<
[flutter_data] [todos] PATCH https://my-json-server.typicode.com/flutterdata/demo/todos/2 [HTTP 200]
 -> body:
 {"id":2,"title":"Todo 2","completed":false}  <<<<<<<<
frank06 commented 2 years ago

Pushed some improved logging, you can now see something like the following in the console:

flutter: 19:999 [watchOne/users#b57beee0@b57e38] initializing (and also watching: todos)
flutter: 20:114   [findOne/users#1@b59013<b57e38] request with {_embed: todos}
flutter: 20:909   [findOne/users#1@b59013<b57e38] {1} fetched from remote
flutter: 20:910   [findOne/users#1@b59013<b57e38]   - with todos {1, 2}
flutter: 25:077 [save/todos#786@b884e9] requesting [HTTP PATCH] https://my-json-server.typicode.com/flutterdata/demo/todos/786
flutter: 25:633 [save/todos#786@b884e9] DataException: {} [HTTP 404] https://my-json-server.typicode.com/flutterdata/demo/todos/786
flutter: 25:637 [save/todos#786@b884e9] custom logging: Todo(id: 786, text: dgfdg, completed: true)

Provided you set ref.todos.logLevel = 2 or higher.

As you noticed you can do some custom logging overriding the onError handler:

onError: (e, label, adapter) async {
  await adapter.onError(e, label); // optional
  adapter.log(
      label!, 'custom logging: ${label.model}');
  ref
      .read(userProvider.notifier)
      .updateWith(exception: e);
});
huang12zheng commented 2 years ago
  1. instead of remote: false, using remote: true in fn save.
  2. override save onSuccess:
    onSuccess: (data, _, __) async {
    await __.onSuccess(data, _);
    __.log(_!, 'custom onSuccess logging: ${_.model}');
    },