supabase / supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
https://supabase.com/
MIT License
734 stars 181 forks source link

Cannot use `SupabaseClient` without dart:html or dart:io in Edge functions #666

Closed maksym-tielnyi-solid closed 1 year ago

maksym-tielnyi-solid commented 1 year ago

Describe the bug The SupabaseClient constructor throws "Cannot create a client without dart:html or dart:io." error when used in Dart Edge functions. An error message looks like the following:

[Error] ub {
  a: "Cannot create a client without dart:html or dart:io.",
  "$thrownJsError": Unsupported operation: Cannot create a client without dart:html or dart:io.
    at Object.J (file:///home/deno/functions/dart_edge/main.dart.js:246:3)
    at Object.v (file:///home/deno/functions/dart_edge/main.dart.js:253:14)
    at Object.wV (file:///home/deno/functions/dart_edge/main.dart.js:2861:15)
    at Object.e2 (file:///home/deno/functions/dart_edge/main.dart.js:2859:14)
    at Object.Xa (file:///home/deno/functions/dart_edge/main.dart.js:2685:3)
    at e.$1 (file:///home/deno/functions/dart_edge/main.dart.js:5492:8)
    at file:///home/deno/functions/dart_edge/main.dart.js:5516:19
    at Gs.a (file:///home/deno/functions/dart_edge/main.dart.js:1401:63)
    at Gs.$2 (file:///home/deno/functions/dart_edge/main.dart.js:3545:14)
    at Object.DI (file:///home/deno/functions/dart_edge/main.dart.js:1381:11)
}

To Reproduce main.dart:

import 'package:edge_http_client/edge_http_client.dart';
import 'package:supabase/supabase.dart';
import 'package:supabase_functions/supabase_functions.dart';

void main() {
  SupabaseFunctions(fetch: (request) {
    final supabaseUrl = Deno.env.get('SUPABASE_URL')!;
    final supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;

    // This SupabaseClient constructor throws an error:
    SupabaseClient(
      supabaseUrl,
      supabaseKey,
      httpClient: EdgeHttpClient(),
    );

    return Response('OK');
  });
}

pubspec.yaml:

name: supabase_flutter_test
description: A sample command-line application.
version: 1.0.0

environment:
  sdk: ^3.0.6

dependencies:
  edge_http_client: ^0.0.1+3
  supabase: 1.11.9
  supabase_functions: 0.0.2+4

dev_dependencies:
  lints: ^2.0.0

Steps to reproduce the behavior:

  1. Build and serve the Edge function.
  2. Call the dart_edge function using Postman/curl/etc.
  3. Observe an Internal Server Error response and an error message in the functions console.

Version: supabase_flutter_test 1.0.0 ├── supabase 1.11.9 │ ├── functions_client 1.3.2 │ ├── gotrue 1.12.4 │ ├── postgrest 1.5.1 │ ├── realtime_client 1.3.0 │ ├── storage_client 1.5.3 └── supabase_functions 0.0.2+4

Additional context flutter doctor output:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.5 22G74 darwin-arm64, locale en-UA)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] IntelliJ IDEA Community Edition (version 2023.1.4)
[✓] VS Code (version 1.82.2)
[✓] Connected device (2 available)
[✓] Network resources

! Doctor found issues in 1 category.
Vinzent03 commented 1 year ago

You need to follow the dart edge documentation. Specifically, you need a custom http client. Either like in the example from the website:

import 'package:edge_http_client/edge_http_client.dart';
import 'package:http/http.dart' as http;

http.runWithClient(() async {
    // Haven't tested this though
    Supabase.initialize();
}, () => EdgeHttpClient());

or simply pass EdgeHttpClient() as httpClient to Supabase.initialize().

This is also shown in the supabase documentation

maksym-tielnyi-solid commented 1 year ago

@Vinzent03 do you mean SupabaseClient instead of Supabase.initialize()? Because the Edge function fails to build when I import supabase_flutter package that contains the Supabase class. I passed EdgeHttpClient to the SupabaseClient constructor, as it is shown in the Supabase documentation and it worked fine until one of the latest updates of the supabase package (probably 1.11.9 or so). I tried to use runWithClient as you suggested and it didn't help as well.

maksym-tielnyi-solid commented 1 year ago

I found that the latest version of realtime_client caused the issue. I added dependency override to 1.2.3 and now Edge functions work fine.

dependency_overrides:
  realtime_client: 1.2.3