Skip to content

fix(supabase_flutter): Safely check if conn is not null to avoid null check operator #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

martinpelli
Copy link

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

After app changes its life cycle, onResumed() method is called to resume any open web socket connection. This code is executed

 if (realtime.channels.isNotEmpty) {
      if (realtime.connState == SocketStates.disconnecting) {
        bool cancel = false;
        final connectFuture = realtime.conn!.sink.done.then(

So, if the SocketStates is disconnecting it will try to use realtime.conn! assuming is not null when it can be null, hence throwing an error Null check operator used on a null value

If we look inside RealtimeClient which holds conn and connState we can see the only time conn is set to null and the only time connState is set to SocketStates.disconnection is here, in the disconnect method

Future<void> disconnect({int? code, String? reason}) async {
    final conn = this.conn;
    if (conn != null) {
      final oldState = connState;
      connState = SocketStates.disconnecting;
      
      
      if (oldState == SocketStates.open ||
          oldState == SocketStates.connecting) {
        if (code != null) {
          await conn.sink.close(code, reason ?? '');
        } else {
          await conn.sink.close();
        }
        connState = SocketStates.disconnected;
        reconnectTimer.reset();
        
      }
      this.conn = null;

      if (heartbeatTimer != null) heartbeatTimer?.cancel();
    }
  }

so it is clear that conn can be null and connState is disconnecting when oldState is closed, disconnecting or disconnected

Fixes #1176

What is the new behavior?

The new behavior just checks if conn is not null.

@Vinzent03
Copy link
Collaborator

Thanks for the pr, but after looking a bit deeper into it, I think this change can be done, but more important is a fix in realtime_client.dart to only set the state to disconnecting if it's connecting or open.

@coveralls
Copy link

coveralls commented May 28, 2025

Pull Request Test Coverage Report for Build 15310644107

Details

  • 3 of 5 (60.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 75.373%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/supabase_flutter/lib/src/supabase.dart 0 2 0.0%
Totals Coverage Status
Change from base Build 15132763952: -0.03%
Covered Lines: 2880
Relevant Lines: 3821

💛 - Coveralls

@Vinzent03 Vinzent03 requested a review from dshukertjr May 28, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

null check operator used on a null value in onResumed()
3 participants