Flutter: Listen to database changes (original) (raw)
- Introduction
- Installing
- Initializing
- Upgrade guide
- Database
- Fetch data
- Insert data
- Update data
- Upsert data
- Delete data
- Call a Postgres function
- Using filters
- Column is equal to a value
- Column is not equal to a value
- Column is greater than a value
- Column is greater than or equal to a value
- Column is less than a value
- Column is less than or equal to a value
- Column matches a pattern
- Column matches a case-insensitive pattern
- Column is a value
- Column is in an array
- Column contains every element in a value
- Contained by value
- Greater than a range
- Greater than or equal to a range
- Less than a range
- Less than or equal to a range
- Mutually exclusive to a range
- With a common element
- Match a string
- Match an associated value
- Don't match the filter
- Match at least one filter
- Match the filter
- Using modifiers
- Return data after inserting
- Order the results
- Limit the number of rows returned
- Limit the query to a range
- Retrieve one row of data
- Retrieve zero or one row of data
- Retrieve as a CSV
- Using explain
- Auth
- Create a new user
- Listen to auth events
- Create an anonymous user
- Sign in a user
- Sign in with ID Token
- Sign in a user through OTP
- Sign in a user through OAuth
- Sign in a user through SSO
- Sign out a user
- Verify and log in through OTP
- Retrieve a session
- Retrieve a new session
- Retrieve a user
- Update a user
- Retrieve identities linked to a user
- Link an identity to a user
- Unlink an identity from a user
- Send a password reauthentication nonce
- Resend an OTP
- Set the session data
- Auth MFA
- Enroll a factor
- Create a challenge
- Verify a challenge
- Create and verify a challenge
- Unenroll a factor
- Get Authenticator Assurance Level
- Auth Admin
- Retrieve a user
- List all users
- Create a user
- Delete a user
- Send an email invite link
- Generate an email link
- Update a user
- Edge Functions
- Invokes a Supabase Edge Function.
- Realtime
- Listen to database changes
- Subscribe to channel
- Unsubscribe from a channel
- Unsubscribe from all channels
- Retrieve all channels
- Storage
- Create a bucket
- Retrieve a bucket
- List all buckets
- Update a bucket
- Delete a bucket
- Empty a bucket
- Upload a file
- Download a file
- List all files in a bucket
- Replace an existing file
- Move an existing file
- Delete files in a bucket
- Create a signed URL
- Retrieve public URL
Returns real-time data from your table as a Stream
.
- Realtime is disabled by default for new tables. You can turn it on by managing replication.
stream()
will emit the initial data as well as any further change on the database asStream<List<Map<String, dynamic>>>
by combining Postgrest and Realtime.- Takes a list of primary key column names that will be used to update and delete the proper records within the SDK.
- The following filters are available
.eq('column', value)
listens to rows where the column equals the value.neq('column', value)
listens to rows where the column does not equal the value.gt('column', value)
listens to rows where the column is greater than the value.gte('column', value)
listens to rows where the column is greater than or equal to the value.lt('column', value)
listens to rows where the column is less than the value.lte('column', value)
listens to rows where the column is less than or equal to the value.inFilter('column', [val1, val2, val3])
listens to rows where the column is one of the values
Examples
Listen to a table
supabase.from('countries')
.stream(primaryKey: ['id'])
.listen((List<Map<String, dynamic>> data) {
// Do something awesome with the data
});
With filter, order and limit
supabase.from('countries')
.stream(primaryKey: ['id'])
.eq('id', 120)
.order('name')
.limit(10);
With an IN filter
supabase.from('countries')
.stream(primaryKey: ['id'])
.inFilter('id', [1, 2, 3])
.order('name')
.limit(10);
Using `stream()` with `StreamBuilder`
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(primaryKey: ['id']);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _stream,
builder: (context, snapshot) {
// Return your widget with the data from the snapshot
},
);
}
}