How do I close a stream when my Get Controller is deleted from memory?

Mike Ottink

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

Mike Ottink

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I get information from the logs on my computer when I only have access to the command line?

분류에서Dev

How do I get PPTP VPN working from my home network when it already works elsewhere?

분류에서Dev

How do I get my JButton to both switch the JPanel and close the JFrame?

분류에서Dev

How do I fix Unity from freezing and artifacts on my screen when I'm charging my laptop?

분류에서Dev

How can I stop my controller from moving my mouse?

분류에서Dev

How do I right-click when connected to my Windows 7 PC from my Android Tablet?

분류에서Dev

How do I SCP from remote machine to local machine when I am outside of my home network?

분류에서Dev

How do i stop my webpage from refreshing when i change the style:display of a div in JS?

분류에서Dev

How do I get my CPU fan to stop spinning when idle?

분류에서Dev

How do I get my Links under my Links label?

분류에서Dev

How do I get my <li> centered in my nav

분류에서Dev

How do I tweet from my bot account when the bot app is created under a separate account?

분류에서Dev

Laravel: how do I pass a value from a form to a controller?

분류에서Dev

How do I access a hard drive from a computer where windows is deleted?

분류에서Dev

How do I get information from a UITableViewCell?

분류에서Dev

How do I stop a windows from closing after I raise wx.EVT_CLOSE in wxPython?

분류에서Dev

How do I get my distro update dialog back?

분류에서Dev

how do I get my partition to mount in the file manager?

분류에서Dev

How do I get my local git stuff into BlueMix?

분류에서Dev

How do I get access permissions right for my backup?

분류에서Dev

How do I get my Activity Indicator to start at the right time?

분류에서Dev

How do I get Ubuntu to detect my Intel Graphics?

분류에서Dev

How do I check how much total memory my VM has?

분류에서Dev

How do I close this JOptionPane upon clicking?

분류에서Dev

Email is successfully sending when ever i close my application

분류에서Dev

How do I prevent my program from printing an extra slash?

분류에서Dev

How do I stop Thunderbird from forgetting my passwords?

분류에서Dev

How do I pass a variable from a shell script to my .muttrc?

분류에서Dev

How can I rewrite my controller URL?

Related 관련 기사

  1. 1

    How do I get information from the logs on my computer when I only have access to the command line?

  2. 2

    How do I get PPTP VPN working from my home network when it already works elsewhere?

  3. 3

    How do I get my JButton to both switch the JPanel and close the JFrame?

  4. 4

    How do I fix Unity from freezing and artifacts on my screen when I'm charging my laptop?

  5. 5

    How can I stop my controller from moving my mouse?

  6. 6

    How do I right-click when connected to my Windows 7 PC from my Android Tablet?

  7. 7

    How do I SCP from remote machine to local machine when I am outside of my home network?

  8. 8

    How do i stop my webpage from refreshing when i change the style:display of a div in JS?

  9. 9

    How do I get my CPU fan to stop spinning when idle?

  10. 10

    How do I get my Links under my Links label?

  11. 11

    How do I get my <li> centered in my nav

  12. 12

    How do I tweet from my bot account when the bot app is created under a separate account?

  13. 13

    Laravel: how do I pass a value from a form to a controller?

  14. 14

    How do I access a hard drive from a computer where windows is deleted?

  15. 15

    How do I get information from a UITableViewCell?

  16. 16

    How do I stop a windows from closing after I raise wx.EVT_CLOSE in wxPython?

  17. 17

    How do I get my distro update dialog back?

  18. 18

    how do I get my partition to mount in the file manager?

  19. 19

    How do I get my local git stuff into BlueMix?

  20. 20

    How do I get access permissions right for my backup?

  21. 21

    How do I get my Activity Indicator to start at the right time?

  22. 22

    How do I get Ubuntu to detect my Intel Graphics?

  23. 23

    How do I check how much total memory my VM has?

  24. 24

    How do I close this JOptionPane upon clicking?

  25. 25

    Email is successfully sending when ever i close my application

  26. 26

    How do I prevent my program from printing an extra slash?

  27. 27

    How do I stop Thunderbird from forgetting my passwords?

  28. 28

    How do I pass a variable from a shell script to my .muttrc?

  29. 29

    How can I rewrite my controller URL?

뜨겁다태그

보관