Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why emitter will call unstoppable while use group to give some params; #18

Closed
CalsRanna opened this issue Nov 8, 2022 · 5 comments
Closed

Comments

@CalsRanna
Copy link

Here's the problem i met.
I need to fetch datas from backend through http, so i write an emitter to fetch datas like this:

final examplesEmitter = Emitter.arg1<List<Example>, Map<String, dynamic>>(
  (ref,credential,emit) async {
    final examples = await ExampleApi().getExamples(credential);
    emit(examples);
  }
)

when i use watch to fetch datas, it will call the api unstoppable even the data has been fetched.

final examples = ref.watch(examplesEmitter({'page': 1, 'pageSize': 50}));

Then i find a solution to solve this for temp.

final examplesEmitter = Emitter.arg2<List<Example>, int, int>(
  (ref,page,pageSize,emit) async {
    final examples = await ExampleApi().getExamples(page, pageSize);
    emit(examples);
  }
)

And the problem solved.

But it confuse me why this problem will occured, and maybe you guys can explain this or something.
Creator is a good state manager, it's simple to understand and use.Hope it goes well.

@liangxianzhe
Copy link
Owner

Hi @CalsRanna , I think this is because creator library checks the argument array using == to determine if two creators are the same. And in Dart, map (and other collections) are only equal if they point to the same object. The following simple program will print false:

  final a = {'page': 1, 'pageSize': 50};
  final b = {'page': 1, 'pageSize': 50};
  print(a == b); // false

In your first example, a new map object is created in every build, and they are not equal to each other. You could put the {'page': 1, 'pageSize': 50} object in a variable outside the Watcher or build function, then it should work.

In your second example, it works because it is simple type and we effectively check 1 == 1 && 50 == 50.

@liangxianzhe
Copy link
Owner

Or you could use const {'page': 1, 'pageSize': 50} so that Dart will use the same object every time.

@CalsRanna
Copy link
Author

Ah, i see.
I was guessing whether it was cause the equal of params or not, seems right.

Here's another wondering.
Since it can be done to warp params into a map or class, is it necessary to provide arg1,arg2 and arg3 to do this?

@liangxianzhe
Copy link
Owner

liangxianzhe commented Nov 8, 2022

If only one argument is provided, people need to define a small data class and implement == and hashCode to make the equality check work. Additional arguments are to make it a little bit more convenient.

@CalsRanna
Copy link
Author

Got it. THX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants