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

What should I do to pass events from provider to view? #21

Closed
tbm98 opened this issue Jun 28, 2020 · 15 comments
Closed

What should I do to pass events from provider to view? #21

tbm98 opened this issue Jun 28, 2020 · 15 comments
Assignees
Labels
enhancement New feature or request

Comments

@tbm98
Copy link
Contributor

tbm98 commented Jun 28, 2020

What should I do to pass events from provider to view? For example from the provider I want to pass an event that displays a dialog or a snackbar.

@tbm98 tbm98 added the enhancement New feature or request label Jun 28, 2020
@rrousselGit
Copy link
Owner

Could you expand on this? I don't understand the question.

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

In a real world example, when the user clicks the login button, I will call the login (username, password) function of the provider. and provider after processing will display the result of success or failure, View will display a dialog message to the user. But in this case, the provider cannot directly display the dialog message.

@rrousselGit
Copy link
Owner

It is not the role of providers to display dialogs.

Make your provider return the result, and have the UI process that result to display things

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

I think if you do that, the view will have to handle a bit of logic. As with MVP architecture, View will only provide an interface to the presenter and the View will not implement any logic.

@rrousselGit
Copy link
Owner

Showing a modal is not business logic but UI logic.
The business logic is to set the error message.

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

in some special cases, an intermittent process and it needs to show the message while it has not finished running. For example, the process of loading my data consists of 5 parts, and after the download is complete, each part will display a message and continue to download the next part, if processed in View, there will need 5 separate functions or At least logic view will have to call at 5 times.
like this

void loadDataClicked() async{
 final aResult = await notifier.load(a);
 if(aResult.isSuccess()){
   showDialog(aResult);
 }else{
 return;
 }

 final bResult = await notifier.load(b);
 if(bResult.isSuccess()){
   showDialog(bResult);
 }else{
 return;
 }

 final cResult = await notifier.load(c);
 if(cResult.isSuccess()){
   showDialog(cResult);
 }else{
 return;
 }

 final dResult = await notifier.load(d);
 if(dResult.isSuccess()){
   showDialog(dResult);
 }else{
 return;
 }

 final eResult = await notifier.load(e);
 if(eResult.isSuccess()){
   showDialog(eResult);
 }else{
 return;
 }
}

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

It would be simpler to write like this:

  void showDialog(...){
    // notifier will call it or notifier send event to View => View call it
  }

  void loadDataClicked() {
    notifier.load(a,b,c,d,e);
  }

@rrousselGit
Copy link
Owner

Maybe expose a Stream and have the UI listen to it to call showDialog.

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

but how can I listen stream (for user not use hook)?

  @override
  Widget build(BuildContext context) {
    Consumer((_, read) {
      print('state is ${read(counterProvider).state}'); //not work
    });
  .....
  @override
  Widget build(BuildContext context) {
    print('state is ${counterProvider.read(context).state}'); // can't read in build 
    ......

@rrousselGit
Copy link
Owner

stream.listen(...) or StreamBuilder

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

Can you write a simple example about listen stream from View ?

@rrousselGit
Copy link
Owner

myProvider.read(context).myStream.listen(...)

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

But it throw Exception when use in build.
where should I put it into if I use stateless ?

@rrousselGit
Copy link
Owner

You need a StatefulWidget for this, or hooks. You cannot listen to an object inside StatelessWidgets.

@tbm98
Copy link
Contributor Author

tbm98 commented Jun 29, 2020

You need a StatefulWidget for this, or hooks. You cannot listen to an object inside StatelessWidgets.

I think it is a best option for now.

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

No branches or pull requests

2 participants