|
1 | | -// import 'dart:async'; |
2 | | -// import 'dart:io'; |
3 | | - |
4 | | -// import 'package:supabase/supabase.dart'; |
5 | | - |
6 | | -// Future<void> main() async { |
7 | | -// const supabaseUrl = ''; |
8 | | -// const supabaseKey = ''; |
9 | | -// final client = SupabaseClient(supabaseUrl, supabaseKey); |
10 | | - |
11 | | -// // query data |
12 | | -// final selectResponse = await client |
13 | | -// .from('countries') |
14 | | -// .select() |
15 | | -// .order('name', ascending: true) |
16 | | -// .execute(count: CountOption.exact); |
17 | | -// if (selectResponse.error == null) { |
18 | | -// print('response.data: ${selectResponse.data}'); |
19 | | -// } |
20 | | - |
21 | | -// // insert data |
22 | | -// final insertResponse = await client.from('countries').insert([ |
23 | | -// {'name': 'Singapore'}, |
24 | | -// ]).execute(); |
25 | | -// if (insertResponse.error == null) { |
26 | | -// print('insertResponse.data: ${insertResponse.data}'); |
27 | | -// } |
28 | | - |
29 | | -// // update data |
30 | | -// final updateResponse = await client |
31 | | -// .from('countries') |
32 | | -// .update({'name': 'Singapore'}) |
33 | | -// .eq('id', 1) |
34 | | -// .execute(); |
35 | | -// if (updateResponse.error == null) { |
36 | | -// print('updateResponse.data: ${updateResponse.data}'); |
37 | | -// } |
38 | | - |
39 | | -// // delete data |
40 | | -// final deleteResponse = |
41 | | -// await client.from('countries').delete().eq('id', 1).execute(); |
42 | | -// if (deleteResponse.error == null) { |
43 | | -// print('deleteResponse.data: ${deleteResponse.data}'); |
44 | | -// } |
45 | | - |
46 | | -// // realtime |
47 | | -// final subscription1 = |
48 | | -// client.from('countries').on(SupabaseEventTypes.delete, (x) { |
49 | | -// print('on countries.delete: ${x.table} ${x.eventType} ${x.oldRecord}'); |
50 | | -// }).subscribe((String event, {String? errorMsg}) { |
51 | | -// print('event: $event error: $errorMsg'); |
52 | | -// }); |
53 | | - |
54 | | -// final subscription2 = client.from('todos').on(SupabaseEventTypes.insert, (x) { |
55 | | -// print('on todos.insert: ${x.table} ${x.eventType} ${x.newRecord}'); |
56 | | -// }).subscribe((String event, {String? errorMsg}) { |
57 | | -// print('event: $event error: $errorMsg'); |
58 | | -// }); |
59 | | - |
60 | | -// // remember to remove subscription |
61 | | -// client.removeSubscription(subscription1); |
62 | | -// client.removeSubscription(subscription2); |
63 | | - |
64 | | -// // stream |
65 | | -// final streamSubscription = client |
66 | | -// .from('countries') |
67 | | -// .stream(['id']) |
68 | | -// .order('name') |
69 | | -// .limit(10) |
70 | | -// .execute() |
71 | | -// .listen((snapshot) { |
72 | | -// print('snapshot: $snapshot'); |
73 | | -// }); |
74 | | - |
75 | | -// // remember to remove subscription |
76 | | -// streamSubscription.cancel(); |
77 | | - |
78 | | -// // Upload file to bucket "public" |
79 | | -// final file = File('example.txt'); |
80 | | -// file.writeAsStringSync('File content'); |
81 | | -// final storageResponse = |
82 | | -// await client.storage.from('public').upload('example.txt', file); |
83 | | -// print('upload response : ${storageResponse.data}'); |
84 | | - |
85 | | -// // Get download url |
86 | | -// final urlResponse = |
87 | | -// await client.storage.from('public').createSignedUrl('example.txt', 60); |
88 | | -// print('download url : ${urlResponse.data}'); |
89 | | - |
90 | | -// // Download text file |
91 | | -// final fileResponse = |
92 | | -// await client.storage.from('public').download('example.txt'); |
93 | | -// if (fileResponse.hasError) { |
94 | | -// print('Error while downloading file : ${fileResponse.error}'); |
95 | | -// } else { |
96 | | -// print('downloaded file : ${String.fromCharCodes(fileResponse.data!)}'); |
97 | | -// } |
98 | | - |
99 | | -// // Delete file |
100 | | -// final deleteFileResponse = |
101 | | -// await client.storage.from('public').remove(['example.txt']); |
102 | | -// print('deleted file id : ${deleteFileResponse.data?.first.id}'); |
103 | | - |
104 | | -// // Local file cleanup |
105 | | -// if (file.existsSync()) file.deleteSync(); |
106 | | -// } |
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:supabase/supabase.dart'; |
| 5 | + |
| 6 | +Future<void> main() async { |
| 7 | + const supabaseUrl = 'YOUR_SUPABASE_URL'; |
| 8 | + const supabaseKey = 'YOUR_ANON_KEY'; |
| 9 | + final supabase = SupabaseClient(supabaseUrl, supabaseKey); |
| 10 | + |
| 11 | + // query data |
| 12 | + final data = |
| 13 | + await supabase.from('countries').select().order('name', ascending: true); |
| 14 | + print(data); |
| 15 | + |
| 16 | + // insert data |
| 17 | + await supabase.from('countries').insert([ |
| 18 | + {'name': 'Singapore'}, |
| 19 | + ]); |
| 20 | + |
| 21 | + // update data |
| 22 | + await supabase.from('countries').update({'name': 'Singapore'}).eq('id', 1); |
| 23 | + |
| 24 | + // delete data |
| 25 | + await supabase.from('countries').delete().eq('id', 1); |
| 26 | + |
| 27 | + // realtime |
| 28 | + final realtimeChannel = supabase.channel('my_channel'); |
| 29 | + realtimeChannel |
| 30 | + .on( |
| 31 | + RealtimeListenTypes.postgresChanges, |
| 32 | + ChannelFilter(event: '*', schema: 'public', table: 'countries'), |
| 33 | + (payload, [ref]) {}) |
| 34 | + .subscribe(); |
| 35 | + |
| 36 | + // remember to remove channel when no longer needed |
| 37 | + supabase.removeChannel(realtimeChannel); |
| 38 | + |
| 39 | + // stream |
| 40 | + final streamSubscription = supabase |
| 41 | + .from('countries') |
| 42 | + .stream(primaryKey: ['id']) |
| 43 | + .order('name') |
| 44 | + .limit(10) |
| 45 | + .listen((snapshot) { |
| 46 | + print('snapshot: $snapshot'); |
| 47 | + }); |
| 48 | + |
| 49 | + // remember to remove subscription |
| 50 | + streamSubscription.cancel(); |
| 51 | + |
| 52 | + // Upload file to bucket "public" |
| 53 | + final file = File('example.txt'); |
| 54 | + file.writeAsStringSync('File content'); |
| 55 | + final storageResponse = |
| 56 | + await supabase.storage.from('public').upload('example.txt', file); |
| 57 | + print('upload response : $storageResponse'); |
| 58 | + |
| 59 | + // Get download url |
| 60 | + final urlResponse = |
| 61 | + await supabase.storage.from('public').createSignedUrl('example.txt', 60); |
| 62 | + print('download url : $urlResponse'); |
| 63 | + |
| 64 | + // Download text file |
| 65 | + final fileResponse = |
| 66 | + await supabase.storage.from('public').download('example.txt'); |
| 67 | + print('downloaded file : ${String.fromCharCodes(fileResponse)}'); |
| 68 | + |
| 69 | + // Delete file |
| 70 | + final deleteFileResponse = |
| 71 | + await supabase.storage.from('public').remove(['example.txt']); |
| 72 | + print('deleted file id : ${deleteFileResponse.first.id}'); |
| 73 | + |
| 74 | + // Local file cleanup |
| 75 | + if (file.existsSync()) file.deleteSync(); |
| 76 | +} |
0 commit comments