Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

fix!: stream replaces the correct row #82

Merged
merged 4 commits into from
Jan 11, 2022

Conversation

Vinzent03
Copy link
Contributor

@Vinzent03 Vinzent03 commented Jan 8, 2022

The stream method needs now the primary key as parameter to detect the updated row correctly.

I've marked the primaryKeys list from 'SupabaseRealtimePayload' as deprecated, because it's on the new RLS Realtime server always empty.

As far as I know, there aren't any Supabase instances running without RLS Realtime anymore, so I think it's okay to force that parameter and don't be backward compatible. We could remove primaryKeys instead of marking it deprecated, too.

close #74, close #78

@Vinzent03
Copy link
Contributor Author

Those lint's error should be fixed, when #81 is merged. I think it will cause merge conflicts, but I will resolve them then.

Copy link
Member

@dshukertjr dshukertjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vinzent03
Thank you so much for an amazing work. I just had one suggestion about primary keys, but everything else looks beautiful!

lib/src/supabase_stream_builder.dart Outdated Show resolved Hide resolved
lib/src/supabase_stream_builder.dart Outdated Show resolved Hide resolved
Copy link
Member

@dshukertjr dshukertjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small things in the comments, but otherwise LGTM!! Thanks for the amazing work!

lib/src/supabase_stream_builder.dart Outdated Show resolved Hide resolved
lib/src/supabase_stream_builder.dart Outdated Show resolved Hide resolved
lib/src/supabase_query_builder.dart Outdated Show resolved Hide resolved
lib/src/supabase_query_builder.dart Outdated Show resolved Hide resolved
lib/src/supabase_stream_builder.dart Show resolved Hide resolved
Co-authored-by: Tyler <18113850+dshukertjr@users.noreply.github.com>
Copy link
Member

@dshukertjr dshukertjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the amazing work!

@dshukertjr dshukertjr merged commit 8361ce4 into supabase:main Jan 11, 2022
@JasonChiu-dev
Copy link

JasonChiu-dev commented Jan 14, 2022

Regarding the subject: stream replaces the correct row, I still encounter the bug after I upgrade to supabase-dart 0.2.12.

(Note: Before upgrade to 0.2.12, the stream worked correctly.)

I found the stream has a bug which cause an error: stream replace the correct row and cause error.

When I insert a row into a table, it shows the following error message:
"The method 'toDouble' was called on null.
Receiver: null
Tried calling: toDouble()"

In fact, I checked the data row I insert into the table is correct as follow:

{projectId: 22-038, itemNumber: 7, itemName: test7, unit: M, unitPrice: 1500.0, contractQuantity: 500.0, contractAmount: 750000.0, remark: , cumulativeCompletedQuantity: 0.0}

which is correct data.

After the insert action is finished, the stream response and shows the data as follow:

{contractAmount: null, contractQuantity: null, cumulativeCompletedQuantity: null, id: 87, itemName: test7, itemNumber: 7, projectId: 22-038, remark: , unit: M, unitPrice: null}

at the same time I get the error message I mentioned above. (
"The method 'toDouble' was called on null.
Receiver: null
Tried calling: toDouble()" )

This is because the stream replace my correct row with wrong row which change my original data to "null".

Then, I check the database and found the correct row {projectId: 22-038, itemNumber: 7, itemName: test7, unit: M, unitPrice: 1500.0, contractQuantity: 500.0, contractAmount: 750000.0, remark: , cumulativeCompletedQuantity: 0.0} still insert into the database successfully. But my App become red screen and shows the error message which should not happen!!

It's so strange!!

Before I upgrade to supabase-dart 0.2.12 with new stream usage by primary-key, the stream worked very good and 100% correct for the same code to insert data to database.

How can I resolve this problem??

@dshukertjr
Copy link
Member

Hi @JasonChiu-dev !

Could we see the code you are using to retrieve the stream?

@JasonChiu-dev
Copy link

JasonChiu-dev commented Jan 14, 2022

Hi @JasonChiu-dev !

Could we see the code you are using to retrieve the stream?

Here is my code to retrieve the stream(NOTE: ALL the code run correctly before supabase-dart 0.2.12):

class MyItem {
// other code of this classs 

MyItem.fromSnapshot(Map<String, dynamic> snapshot)
      : id = snapshot['id'],
        projectId = snapshot['projectId'],
        itemNumber = snapshot['itemNumber'],
        itemName = snapshot['itemName'],
        unit = snapshot['unit'],
        unitPrice = snapshot['unitPrice'].toDouble(),
        contractQuantity = snapshot['contractQuantity'].toDouble(),
        contractAmount = snapshot['contractAmount'].toDouble(),
        remark = snapshot['remark'],
        cumulativeCompletedQuantity = snapshot['cumulativeCompletedQuantity'].toDouble();

// other code of this class ...
}

List<MyItem> getMyItemsFromQuery(List<Map<String, dynamic>> snapshot) {
    _items = []; // Type: MyItem
    
    for (final doc in snapshot) {
      // **Todo: check Supabase-dart 0.2.12 stream error: stream replace the correct row with null on some columns!!**
      
      _items.add(MyItem.fromSnapshot(doc));
    }

    return _items;
  }

// here is the StreamBuilder code sample:
StreamBuilder<List<Map<String, dynamic>>>(
      stream: _supabase.from('myItems:projectId=eq.$projectId').stream(['id']).order('id', ascending: true).execute(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return Center(
            child: Text(snapshot.error.toString()),
          );
        }

        // if (!snapshot.hasData || snapshot.connectionState == ConnectionState.waiting) {
        if (!snapshot.hasData) {
          return const Center(child: CupertinoActivityIndicator());
        }

      
        List<MyItem> items = getMyItemsFromQuery(snapshot.requireData);
......

// end of StreamBuilder sample code

(NOTE: ALL the code run correctly before supabase-dart 0.2.12)

@JasonChiu-dev
Copy link

Is there anyone who know how to solve this stream bug?

@phamhieu
Copy link
Member

@JasonChiu-dev you can try to revert supabase-dart to v0.2.10 first if it worked for you previously

@phamhieu
Copy link
Member

phamhieu commented Jan 14, 2022

i created new issue. let keep track the update here

@JasonChiu-dev
Copy link

JasonChiu-dev commented Jan 14, 2022

Hi Hieu,

Thank you so much for your response.

Regarding: "you can try to revert supabase-dart to v0.2.10 first if it worked for you previously", I did revert supabase-dart to v0.2.10 and the stream work correckly, but the other error occured, as follow:

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast
#0 RealtimeSubscription.onError. (package:realtime_client/src/realtime_subscription.dart:97:42)
#1 RealtimeSubscription.trigger (package:realtime_client/src/realtime_subscription.dart:225:20)
#2 RealtimeClient.onConnMessage.. (package:realtime_client/src/realtime_client.dart:267:34)

......

I think we do need fully handle the stream bug and make things go right.

For your information.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants