tanutapi / dart_meteor

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

listen meteor.collections #24

Closed YouSour closed 3 years ago

YouSour commented 3 years ago

hi , bro @tanutapi , is it possible to listen meteor.collections['test'] ? thank in advance :)

tanutapi commented 3 years ago

With the version 1.x.x, you have to call meteor.prepareCollection('test') before you can access it. Make sure that your serverside code publish a test collection.

YouSour commented 3 years ago

here, i did @tanutapi but it still not work

on Server Side

import { Meteor } from 'meteor/meteor'

// Collection
import { MobileNotification } from '../../../common/collections/mobile-notification'

Meteor.publish('microfis_mobileNotification', function() {
    if (this.userId) {
        let data = MobileNotification.find();
        return data;
    }
    return this.ready();
});

on Flutter

@override
  void initState() {
    super.initState();
    meteor.prepareCollection('microfis_mobileNotification');
    meteor.collections['microfis_mobileNotification'].listen((event) {
      print('im listening ::::>>>>> $event');
    });
  }
tanutapi commented 3 years ago

On the Flutter side, make sure that you have subscribed to your publication.

...
class _YourWidgetState extends State<YourWidget> {
  SubscriptionHandler _subscriptionHandler;

  @override
  void initState() {
    super.initState();
    meteor.prepareCollection('test');
    _subscriptionHandler = meteor.subscribe('microfis_mobileNotification', []);
  }

  @override
  void dispose() {
    super.dispose();
    _subscriptionHandler.stop();
  }
}
...
YouSour commented 3 years ago

thank you so much, it works @tanutapi 😄