tanutapi / dart_meteor

A Meteor DDP library for Dart/Flutter developers.
Other
37 stars 15 forks source link

how to stop meteor.collections listen when go to other screen ? #28

Closed YouSour closed 3 years ago

YouSour commented 3 years ago

hi , @tanutapi , i got some problem with meteor.collections listen , i try to stop listen to data when i go to other screen , but i don't know how to stop it?

class _SaleState extends State<Sale> {
  SubscriptionHandler _subscriptionHandler;
  Map<String, dynamic> _branchDoc;
  Map<String, dynamic> _departmentDoc;

  @override
  void initState() {
    super.initState();
    meteor.prepareCollection('rest_sales');
  }

  void realTimeSaleData() async {
    _branchDoc = await BranchStorage().getUserBranches();
    _departmentDoc = await DepartmentStorage().getUserDepartment();

    _subscriptionHandler = meteor.subscribe('sales', [
      {
        'branchId': _branchDoc['_id'],
        'depId': _departmentDoc['_id'],
        'status': "Open"
      }
    ], onReady: () {
      meteor.collections['rest_sales'].listen((event) {
        print('sale ::>> $event');

      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _subscriptionHandler.stop();
  }
 @override
  Widget build(BuildContext context) {
    realTimeSaleData();
...
keepjhonnying commented 3 years ago

Hey @YouSour How do you resolve this problem?

tanutapi commented 3 years ago

Basically, we do a subscription in the initState() and stop the subscription in dispose() of the StatefulWidget.

tanutapi commented 3 years ago

@keepjhonnying you may read my article on Medium, https://link.medium.com/81anzxcGJib.

YouSour commented 3 years ago

@keepjhonnying dispose() on subscription and cancel() on collection listen , it completely shut down 😄 Example :

  SubscriptionHandler _projectSubscriptionHandler;
  StreamSubscription<Map<String, dynamic>> _projectCollectionListener;

if (_projectMemberSubscriptionHandler != null)
_projectMemberSubscriptionHandler.stop();
 _projectMemberCollectionListener.cancel();