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
698 stars 163 forks source link

Unexpected behavior in realtime events in Flutter android app #706

Open B0yma opened 10 months ago

B0yma commented 10 months ago

Bug report

Describe the bug

Unexpected behavior in flutter android app. in some cases realtime events on update not fired. in some cases fired with wrong data

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. create table with rls policy

    create table
    public.test (
    id bigint generated by default as identity,
    some_int bigint null,
    constraint test_pkey primary key (id)
    ) tablespace pg_default;
    
    BEGIN;
    ALTER POLICY "Enable read access for all users" ON "public"."test" USING ((some_int >= 1));
    COMMIT;
  2. put rows with some_int - [0, 1, 2]

  3. run flutter app with your secrets

    
    Future<void> main() async {
    final supabase = await Supabase.initialize(
    url: supabaseUrl,
    anonKey: supabaseAnonKey,
    );
    runApp(MyApp(supabase: supabase));
    }

class MyApp extends StatelessWidget {

final Supabase supabase; const MyApp( {super.key, required this.supabase});

@override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: StreamBuilder<List>( stream: getSomeInts(), initialData: const [], builder: (context, snapshot) { return const FlutterLogo(); } ) ); }

Stream<List> getSomeInts() { return supabase.client.from('test').stream(primaryKey: ['id']).map((event) { log("event.toString():$event"); return event.map((e) => e['some_int'] as int).toList(); }); } }

you will see in log 

event.toString():[{id: 2, some_int: 1}, {id: 3, some_int: 2}]

4. change in supbase some_int from 0 -> 3
you will see in log 

event.toString():[{id: 2, some_int: 1}, {id: 3, some_int: 2}, {id: 1, some_int: 3}]


5. change in supbase some_int from 3 -> 0
log not fires
## Expected behavior
event fires

6. change in supbase some_int from 1 -> 3
you will see in log 

event.toString():[{id: 2, some_int: 3}, {id: 3, some_int: 2}, {id: 1, some_int: 3}]


its wrong data
## Expected behavior
correct data [0,3,2]

## Screenshots
![image](https://github.com/supabase/supabase/assets/14963010/549906d1-e1c4-49c0-af12-d0c30515e0a8)
![image](https://github.com/supabase/supabase/assets/14963010/e3d037d7-f507-4693-b788-9ec90d61b489)

## System information
- OS: Windows 10

## Additional context
flutter doctor
- Doctor summary (to see all details, run flutter doctor -v):
- [√] Flutter (Channel stable, 3.13.6, on Microsoft Windows [Version 10.0.19045.2251]
- Android Studio (version 2022.3)

version of supabse in pubspec of flutter
- supabase_flutter: ^1.10.23

device 
- android emulator api33_x86_64
B0yma commented 9 months ago

up

dshukertjr commented 9 months ago

This is the expected behavior currently.

Because you have set the RLS so that some_int less than 1 is not readable, the update on step 5 is not readable from the client.

We are going to need to wait for the fix on the server side for events like this to be sent to the client. Related https://github.com/supabase/walrus/issues/64