Skip to content

Commit 3c8d4db

Browse files
authored
fix: Prevent web incompatibility due to import of dart:io (#1220)
* feat: add mini dart web app example * fix: use stub file for dart:io usage * fix: keep getter in constants class * chore: adjust sdk version * fix: remove alias import and add web assets * chore: allow any version * chore: clean up
1 parent 53a2427 commit 3c8d4db

File tree

12 files changed

+175
-88
lines changed

12 files changed

+175
-88
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: supabase_example
2+
description: An absolute bare-bones supabase web app.
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: '>=3.3.0 <4.0.0'
8+
9+
dependencies:
10+
web: '>=0.5.0 <2.0.0'
11+
supabase: ^2.9.0
12+
13+
dev_dependencies:
14+
build_runner: any
15+
build_web_compilers: any
16+
lints: ^4.0.0
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<meta name="scaffolded-by" content="https://github.com/dart-lang/sdk" />
9+
<title>dart_web_app</title>
10+
<link rel="stylesheet" href="styles.css" />
11+
<script defer src="main.dart.js"></script>
12+
</head>
13+
14+
<body>
15+
<div id="output"></div>
16+
</body>
17+
</html>
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
html,
2+
body {
3+
width: 100%;
4+
height: 100%;
5+
margin: 0;
6+
padding: 0;
7+
}
8+
9+
#output {
10+
padding: 20px;
11+
text-align: center;
12+
}

packages/supabase/lib/src/constants.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import 'package:supabase/src/version.dart';
2-
import 'dart:io' show Platform;
3-
4-
const bool kIsWeb = bool.fromEnvironment('dart.library.js_util');
2+
import 'platform_stub.dart' if (dart.library.io) 'platform_io.dart';
53

64
class Constants {
7-
static String? get platform {
8-
return kIsWeb ? null : Platform.operatingSystem;
9-
}
10-
11-
static String? get platformVersion {
12-
return kIsWeb ? null : Platform.operatingSystemVersion;
13-
}
5+
static String? get platform => condPlatform;
6+
static String? get platformVersion => condPlatformVersion;
147

158
static final Map<String, String> defaultHeaders = {
169
'X-Client-Info': 'supabase-dart/$version',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'dart:io';
2+
3+
String? get condPlatform {
4+
return Platform.operatingSystem;
5+
}
6+
7+
String? get condPlatformVersion {
8+
return Platform.operatingSystemVersion;
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
String? get condPlatform {
2+
return null;
3+
}
4+
5+
String? get condPlatformVersion {
6+
return null;
7+
}

0 commit comments

Comments
 (0)