由于我的 BaaS,我想将实时数据和 StreamBuilder 与名为 realtime_dart 的包一起使用。Supabase 使用这个包,所以我也需要它。但我不知道如何以正确的方式使用 Socket 钩子。
这是我正在使用的包的示例(realtime_dart)
import 'package:realtime_client/realtime_client.dart';
/// Example to use with Supabase Realtime https://supabase.io/
Future<void> main() async {
final socket = RealtimeClient('ws://SUPABASE_API_ENDPOINT/realtime/v1',
params: {'apikey': 'SUPABSE_API_KEY'},
// ignore: avoid_print
logger: (kind, msg, data) => {print('$kind $msg $data')});
final channel = socket.channel('realtime:public');
channel.on('DELETE', (payload, {ref}) {
print('channel delete payload: $payload');
});
channel.on('INSERT', (payload, {ref}) {
print('channel insert payload: $payload');
});
socket.onMessage((message) => print('MESSAGE $message'));
// on connect and subscribe
socket.connect();
channel.subscribe().receive('ok', (_) => print('SUBSCRIBED'));
// delay 20s to receive events from server
await Future.delayed(const Duration(seconds: 20));
// on unsubscribe and disconnect
channel.unsubscribe();
socket.disconnect();
}
如何将这个套接字挂钩与 StreamBuilder 一起使用?
谢谢