'Flutter Signalr Listener is not connected in 2nd screen after migrated to null safety stable version

Chatting was working perfectly before migrating to null safety using signalr. But after migrating It is not working in chatting part.

Scenario is like there are 2 screens where I am using signalr. 1)Chatlist. 2)Chatting with person.

listener in Chatlist is perfect but in 2nd screen it is not working(Just worked when I installed and run for the 1st time). Weird issue.

All was working in old. I am using bloc for statemanagement and also migrated to yield to emit.

Piece of code is like:

 void listenOnMessageReceived(
      HubConnection hubConnection,
      Function(Message? chatMessageReceive) onMessageReceived,
      ) {
    final SocketResponseCallBack chatMessageReceived =
        (response) => onMessageReceived(Message.fromJson(response));
    final hubMethod = HubMethod(
        CHAT_RECEIVED_MESSAGE_METHOD_NAME,
        SignalRHelper.toSocketFunction(
            CHAT_RECEIVED_MESSAGE_METHOD_NAME, chatMessageReceived));

    bool exists = listenOnHubMethod.any((method) => method.methodName == CHAT_RECEIVED_MESSAGE_METHOD_NAME);
    if(exists) {
      listenOnHubMethod.removeWhere((element) =>
      element.methodName == CHAT_RECEIVED_MESSAGE_METHOD_NAME);
      SignalRHelper(hubConnection: hubConnection).on(
        hubMethod.methodName,
        hubMethod.methodFunction,
      );
      listenOnHubMethod.add(hubMethod);
    }else{
      SignalRHelper(hubConnection: hubConnection).on(
        hubMethod.methodName,
        hubMethod.methodFunction,
      );
      listenOnHubMethod.add(hubMethod);
    }
  }

I am having 2 types of above code in different screens. but it is working in only 1 screen and not listening in 2nd screen.

here is a piece of signalr listener code:

  static MethodInvocationFunc toSocketFunction(
      String methodName, SocketResponseCallBack responseCallBack) {
    return (arguments) {
      try {
        if (arguments!.isEmpty) {
          throw SocketEmptyResponseException(methodName);
        }
        final response = arguments.first;
        responseCallBack(response);
      } on FormatException {
        throw SocketResponseException(methodName);
      }
    };
  }

Is there any limitations in migration of stable version or anything else. Every help is appreciable. Thank you.



Solution 1:[1]

Do not use signalR, on IOS it will be impossible to run listener on background or when app is closed and you will miss messages. Use FCM.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nazarii Boichyshyn