I am using the following package https://pub.dev/packages/get .
I have the following controller
class GroupController extends GetController{
StreamController<GroupModel> _streamController = BehaviorSubject();
Stream<GroupModel> get stream => _streamController.stream;
GroupController(DatabaseService database, GroupModel group)
{
_streamController.addStream(database.groupStream(group));
}
@override
void dispose(){
print('dispose');
_streamController.close();
super.dispose();
}
}
But when I call it the dispose is never called. I call it like this
GetBuilder<GroupController>(
init: GroupController(database, _group),
builder: (GroupController groupController) => StreamBuilder(
stream: groupController.stream,
builder: (BuildContext context, AsyncSnapshot<GroupModel> groupSnapshot) {
I want my controller's dispose method being called whenever the controller is removed from memory.
I noticed that the GetBuilder has a dispose callback. And it requires the state from a widget. So do I need to make the widget that holds the controller statefull? Or do I need to pass a new statefull widget that holds the state? The documentation is not all to clear on it. How do I call the dispose on my controller?
I noticed there is a console log whenever a controller get's deleted from memory, isn't there a callback so I can close the stream there? I would really like to avoid making the widget statefull tho.
Edit
class GroupController extends GetController{
StreamController<GroupModel> _streamController = BehaviorSubject();
Stream<GroupModel> get stream => _streamController.stream;
GroupController(DatabaseService database, GroupModel group)
{
_streamController.addStream(database.groupStream(group));
}
@override
void close(){
print('log if close is invoked');
_streamController.close();
super.close();
}
}
Generate the following log
I/flutter (23404): log if close is invoked
I/flutter (23404): Close can't be called
I/flutter (23404): [GET] GroupController deleted from memory
Somehow it error when trying to close the controller
This is probably how you want to be doing it. Not adding the stream in the contructor because this causes problems when the controller get's rebuild. Because it will add the stream to the contructor once more.
import 'dart:async';
import 'package:get/get.dart';
import 'package:rxdart/rxdart.dart';
class GetStreamController<T> extends GetController {
final Stream<T> Function() value;
StreamController<T> _streamController = BehaviorSubject();
StreamSubscription _streamSubscription;
Stream<T> get stream => _streamController.stream;
GetStreamController(this.value);
@override
void onInit()
{
super.onInit();
_streamSubscription = value().listen((event) {
_streamController.add(event);
});
}
@override
void onClose()
{
super.onClose();
_streamSubscription.cancel();
_streamController.close();
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다