Skip to content

Commit cac01bb

Browse files
committed
feat: add mini dart web app example
1 parent 9b42fab commit cac01bb

File tree

6 files changed

+127
-78
lines changed

6 files changed

+127
-78
lines changed

.github/workflows/supabase.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,9 @@ jobs:
7070

7171
- name: Run tests
7272
run: dart test --concurrency=1
73+
74+
- name: Build Dart web app
75+
run: |
76+
dart pub global activate webdev
77+
cd example
78+
webdev build
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+

packages/supabase/example/main.dart

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: supabase_example
2+
description: An absolute bare-bones supabase web app.
3+
version: 1.0.0
4+
# repository: https://github.com/my_org/my_repo
5+
publish_to: none
6+
7+
environment:
8+
sdk: ^3.9.0
9+
10+
# Add regular dependencies here.
11+
dependencies:
12+
web: ^1.1.1
13+
supabase: ^2.9.0
14+
15+
dev_dependencies:
16+
build_runner: ^2.4.15
17+
build_web_compilers: ^4.1.6
18+
lints: ^6.0.0
19+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:supabase/supabase.dart';
4+
import 'package:web/web.dart' as web;
5+
6+
void main() {
7+
const supabaseUrl = 'YOUR_SUPABASE_URL';
8+
const supabaseKey = 'YOUR_ANON_KEY';
9+
final supabase = SupabaseClient(supabaseUrl, supabaseKey);
10+
11+
final element = web.document.querySelector('#output') as web.HTMLDivElement;
12+
element.textContent = 'Supabase Dart Web Example';
13+
14+
exampleUsage(supabase);
15+
}
16+
17+
void exampleUsage(SupabaseClient supabase) async {
18+
// query data
19+
final data = await supabase
20+
.from('countries')
21+
.select()
22+
.order('name', ascending: true);
23+
print(data);
24+
25+
// insert data
26+
await supabase.from('countries').insert([
27+
{'name': 'Singapore'},
28+
]);
29+
30+
// update data
31+
await supabase.from('countries').update({'name': 'Singapore'}).eq('id', 1);
32+
33+
// delete data
34+
await supabase.from('countries').delete().eq('id', 1);
35+
36+
// realtime
37+
final realtimeChannel = supabase.channel('my_channel');
38+
realtimeChannel
39+
.onPostgresChanges(
40+
event: PostgresChangeEvent.all,
41+
schema: 'public',
42+
table: 'countries',
43+
callback: (payload) {},
44+
)
45+
.subscribe();
46+
47+
// remember to remove channel when no longer needed
48+
supabase.removeChannel(realtimeChannel);
49+
50+
// stream
51+
final streamSubscription = supabase
52+
.from('countries')
53+
.stream(primaryKey: ['id'])
54+
.order('name')
55+
.limit(10)
56+
.listen((snapshot) {
57+
print('snapshot: $snapshot');
58+
});
59+
60+
// remember to remove subscription
61+
streamSubscription.cancel();
62+
63+
// Upload file to bucket "public" with dart:io
64+
65+
// final file = File('example.txt');
66+
// file.writeAsStringSync('File content');
67+
// final storageResponse = await supabase.storage
68+
// .from('public')
69+
// .upload('example.txt', file);
70+
71+
// Upload file to bucket "public" without dart:io
72+
final content = "my file content";
73+
final storageResponse = await supabase.storage
74+
.from('public')
75+
.uploadBinary('example.txt', Uint8List.fromList(content.codeUnits));
76+
print('upload response : $storageResponse');
77+
78+
// Get download url
79+
final urlResponse = await supabase.storage
80+
.from('public')
81+
.createSignedUrl('example.txt', 60);
82+
print('download url : $urlResponse');
83+
84+
// Download text file
85+
final fileResponse = await supabase.storage
86+
.from('public')
87+
.download('example.txt');
88+
print('downloaded file : ${String.fromCharCodes(fileResponse)}');
89+
90+
// Delete file
91+
final deleteFileResponse = await supabase.storage.from('public').remove([
92+
'example.txt',
93+
]);
94+
print('deleted file id : ${deleteFileResponse.first.id}');
95+
96+
// Local file cleanup on dart:io
97+
// if (file.existsSync()) file.deleteSync();
98+
}

packages/supabase_flutter/example/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ migrate_working_dir/
3535
/build/
3636

3737
# Web related
38-
lib/generated_plugin_registrant.dart
3938

4039
# Symbolication related
4140
app.*.symbols

0 commit comments

Comments
 (0)