PatilShreyas / LiveStream-Flutter

Dart package to which makes data communication easy among different modules of your application.
MIT License
75 stars 10 forks source link

how to update UI in callback? #7

Open lucasjinreal opened 4 years ago

lucasjinreal commented 4 years ago

I using a callback, when livestream gives me an emit message, can I need call setState to update UI,

but there was any error everytime I try to update UI:

E/flutter (19781): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (19781): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (19781): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter (19781): #0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1112:9)
E/flutter (19781): #1      State.setState (package:flutter/src/widgets/framework.dart:1147:6)
E/flutter (19781): #2      _ChatPageState._solveReceivedMessage (package:tunnel/tab_page/tab1_children/chat_page3.dart:396:9)
E/flutter (19781): #3      _ChatPageState.initState.<anonymous closure> (package:tunnel/tab_page/tab1_children/chat_page3.dart:216:7)
E/flutter (19781): #4      _DataStore.setValue.<anonymous closure> (package:livestream/livestream.dart:54:15)

what should we properly update UI in callback?

PatilShreyas commented 4 years ago

Example is given here in this repository: https://github.com/PatilShreyas/LiveStream-Flutter/tree/master/example/lib/main.dart

On Sun 15 Mar, 2020, 9:18 AM JinTian, notifications@github.com wrote:

I using a callback, when livestream gives me an emit message, can I need call setState to update UI,

but there was any error everytime I try to update UI:

E/flutter (19781): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. E/flutter (19781): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. E/flutter (19781): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose(). E/flutter (19781): #0 State.setState. (package:flutter/src/widgets/framework.dart:1112:9) E/flutter (19781): #1 State.setState (package:flutter/src/widgets/framework.dart:1147:6) E/flutter (19781): #2 _ChatPageState._solveReceivedMessage (package:tunnel/tab_page/tab1_children/chat_page3.dart:396:9) E/flutter (19781): #3 _ChatPageState.initState. (package:tunnel/tab_page/tab1_children/chat_page3.dart:216:7) E/flutter (19781): #4 _DataStore.setValue. (package:livestream/livestream.dart:54:15)

what should we properly update UI in callback?

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/PatilShreyas/LiveStream-Flutter/issues/7, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVWFOGEO74T2VGNLHJQ463RHRF2VANCNFSM4LKLL2LQ .

lucasjinreal commented 4 years ago

thanks for your quick reply!

livestream works perfectly on emit messages and receive, but I think I just found a bug of it, let me explain it:

In my case, I have a page need enter and out many times, it's a conversation chat UI page, once my global websocket controller received new message, it will emit to that page if user currently chating.

but weired thing is that, if the page opened once, the UI will update if received msg, then I out and re-enter that page, if new msg come, UI won't update.

I think it's because of livestream binding the callback only once, but what if I have a same page need enter manytimes for example: a chat UI page, many contacts share a same page code, currently it only update once correct, does there any furthere reason for this ?

lucasjinreal commented 4 years ago

Actually I try to call like this:

 _liveStream.on("p2p-msg", (data) {
      print('[live stream] message emit from gWebsocketController. ${data}');
      setState(() {
        _solveReceivedMessage(data);
      });
    });

It will raise error like above.

PatilShreyas commented 4 years ago

Okay. I'll check.

lucasjinreal commented 4 years ago

@PatilShreyas thanks for your patience. the one page example I tested it's ok, hope you got some clues.