Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

dart - Flutter GetXController : "Unhandled Exception: '!isClosed': You cannot add event to closed Stream"

Couple of hour I have been struggling with this issue. There are some solution for BLoC and StreamController but there is no proper solution for GetX.

As their suggestion from BloC answer, I got an idea that, it might be related to GetXController dispose. Then I apply dispose() method for controller in my page where I initialized the user_controller. But still having the error like below

Unhandled Exception: 'package:get/get_rx/src/rx_stream/get_stream.dart': Failed assertion: line 90 pos 12: '!isClosed': You cannot add event to closed Stream
E/flutter (21576): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter (21576): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (21576): #2      GetStream.add (package:get/get_rx/src/rx_stream/get_stream.dart:90:12)
E/flutter (21576): #3      NotifyManager.addListener.<anonymous closure> (package:get/get_rx/src/rx_types/rx_core/rx_impl.dart:139:15)
E/flutter (21576): #4      GetStream._notifyData (package:get/get_rx/src/rx_stream/get_stream.dart:47:21)
E/flutter (21576): #5      GetStream.add (package:get/get_rx/src/rx_stream/get_stream.dart:92:5)
E/flutter (21576): #6      RxObjectMixin.value= (package:get/get_rx/src/rx_types/rx_core/rx_impl.dart:101:13)
E/flutter (21576): #7      UserController.getUserData (package:hellobabybox/controllers/user_controller.dart:20:12)

Thanks in Advance

question from:https://stackoverflow.com/questions/66064080/flutter-getxcontroller-unhandled-exception-isclosed-you-cannot-add-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have never used that library myself, but I think this will give you some idea.

StreamController controller = ...;
if (!controller.isClosed) {
  controller.sink.add(event); // Only add the event if the stream isn't closed. 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...