Files
supabase/apps/reference/_supabase_dart/generated/stream.mdx
T

106 lines
2.4 KiB
Plaintext

---
id: stream
title: 'stream()'
slug: /stream
custom_edit_url: https://github.com/supabase/supabase/edit/master/spec/supabase_dart_v1_legacy.yml
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
Notifies of data at the queried table.
```dart
supabase.from('countries')
.stream(['id'])
.listen((List<Map<String, dynamic>> data) {
// Do something awesome with the data
});
```
## Notes
- `stream()` will emit the initial data as well as any further change on the database as `Stream` of `List<Map<String, dynamic>>` by combining Postgrest and Realtime.
- Takes a list of primary key columns as its argument.
## Examples
### Listening to a specific table
```dart
supabase.from('countries')
.stream(['id'])
.listen((List<Map<String, dynamic>> data) {
// Do something awesome with the data
});
```
### Listening to a specific rows within a table
You can add up to one `eq` filter to your `stream()` query.
```dart
supabase.from('countries')
.stream(['id'])
.eq('id', '120')
.listen((List<Map<String, dynamic>> data) {
// Do something awesome with the data
});
```
### With `order()`
Allows you to order the queried data by the value of the specified column.
```dart
supabase.from('countries')
.stream(['id'])
.order('name', ascending: true)
.listen((List<Map<String, dynamic>> data) {
// Do something awesome with the data
});
```
### With `limit()`
Allows you to limit the number of rows returned.
```dart
supabase.from('countries')
.stream(['id'])
.order('name', ascending: true)
.limit(10)
.listen((List<Map<String, dynamic>> data) {
// Do something awesome with the data
});
```
### Using `stream()` with `StreamBuilder`
When using `stream()` with a `StreamBuilder` within your Flutter application, make sure to store your stream in a variable to prevent refetching upon rebuilding.
```dart
final supabase = Supabase.instance.client;
class MyWidget extends StatefulWidget {
const MyWidget({Key? key}) : super(key: key);
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
// Persist the stream in a local variable to prevent refetching upon rebuilds
final _stream = supabase.from('countries').stream(['id']);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _stream,
builder: (context, snapshot) {
// Return your widget with the data from the snapshot
},
);
}
}
```