Flutter and rethinkDB - Code for changes?

160 views
Skip to first unread message

Meg Lee Chin

unread,
Jan 31, 2021, 4:02:29 AM1/31/21
to RethinkDB
I'd like to use the Dart Rethink driver in Flutter to update my app in realtime with database updates.

https://pub.dev/packages/rethinkdb_dart

I need a real-time updated JSON list that I can use to update a listview in Streambuilder.

But I'm not sure how to get a change feed from my Rethinkdb Server;

void main(List<String> arguments) async { var connection; try { connection = await r.connect(db: "test", host: "myhostip", port: 28015); var changes = await r.table('tv_shows').changes().run(connection); print("changes: $changes");

The above code simply writes changes: Instance of 'Feed' to my console;

Any ideas what I should do?

Kokou AHIANYO

unread,
Dec 31, 2022, 2:45:25 PM12/31/22
to RethinkDB
Try this code. It's working for me fine.

final _controller = StreamController<Map>.broadcast();
StreamSubscription_changeFeed;

Stream<Map> getMapData(){
         _getJson();
        return _controller.stream;
}

_getJson(){
   _changeFeed =  r.table('tv_shows')
      .changes({'include_initiale': true})
     .run(connection)
    .asStream()
    .cast<Feed>()
    .listen((e){
          e.forEach((feed){
               if(feed['new_val'] == null) return null;
              _controller.sink.add(feed['new_val']);
           })
            .catchError((err) => print(err))
            .onError((error, stackTrace) => print(error));
       },
     );
}

dispose(){
     _controller?.close();
     _changeFeed?.cancel();
Reply all
Reply to author
Forward
0 new messages