|
| 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 | +} |
0 commit comments