Flutter: Subscribe to channel

Subscribe to realtime changes in your database.

Examples

Listen to all database changes

supabase.channel('*').on(
  RealtimeListenTypes.postgresChanges,
  ChannelFilter(event: '*', schema: '*'),
  (payload, [ref]) \{
    print('Change received: $\{payload.toString()\}');
  \},
).subscribe();

Listen to a specific table

supabase.channel('public:countries').on(
  RealtimeListenTypes.postgresChanges,
  ChannelFilter(event: '*', schema: 'public', table: 'countries'),
  (payload, [ref]) \{
    print('Change received: $\{payload.toString()\}');
  \},
).subscribe();

Listen to inserts

supabase.channel('public:countries').on(
  RealtimeListenTypes.postgresChanges,
  ChannelFilter(event: 'INSERT', schema: 'public', table: 'countries'),
  (payload, [ref]) \{
    print('Change received: $\{payload.toString()\}');
  \},
).subscribe();

Listen to updates

supabase.channel('public:countries').on(
  RealtimeListenTypes.postgresChanges,
  ChannelFilter(event: 'UPDATE', schema: 'public', table: 'countries'),
  (payload, [ref]) \{
    print('Change received: $\{payload.toString()\}');
  \},
).subscribe();

Listen to deletes

supabase.channel('public:countries').on(
  RealtimeListenTypes.postgresChanges,
  ChannelFilter(event: 'DELETE', schema: 'public', table: 'countries'),
  (payload, [ref]) \{
    print('Change received: $\{payload.toString()\}');
  \},
).subscribe();

Listen to multiple events

supabase.channel('public:countries').on(RealtimeListenTypes.postgresChanges,
    ChannelFilter(event: 'INSERT', schema: 'public', table: 'countries'),
    (payload, [ref]) \{
  print('Change received: $\{payload.toString()\}');
\}).on(RealtimeListenTypes.postgresChanges,
    ChannelFilter(event: 'DELETE', schema: 'public', table: 'countries'),
    (payload, [ref]) \{
  print('Change received: $\{payload.toString()\}');
\}).subscribe();

Listen to row level changes

supabase.channel('public:countries:id=eq.200').on(
    RealtimeListenTypes.postgresChanges,
    ChannelFilter(
      event: 'UPDATE',
      schema: 'public',
      table: 'countries',
      filter: 'id=eq.200',
    ), (payload, [ref]) \{
  print('Change received: $\{payload.toString()\}');
\}).subscribe();