createEvents method Null safety

void createEvents(
  1. AgoraEventHandlers? agoraEventHandlers
)

Implementation

void createEvents(AgoraEventHandlers? agoraEventHandlers) async {
  value.engine?.setEventHandler(
    RtcEngineEventHandler(
      error: (code) {
        final info = 'onError: $code';
        print(info);
        var onErrorFun = agoraEventHandlers?.onError;
        if (onErrorFun != null) onErrorFun(code);
      },
      joinChannelSuccess: (channel, uid, elapsed) {
        final info = 'onJoinChannel: $channel, uid: $uid';
        print(info);
        value = value.copyWith(localUid: uid);
        value = value.copyWith(
          mainAgoraUser: AgoraUser(
            uid: uid,
            remote: false,
            muted: value.isLocalUserMuted,
            videoDisabled: value.isLocalVideoDisabled,
            clientRole: value.clientRole,
          ),
        );
        var joinChannelSuccessFun = agoraEventHandlers?.joinChannelSuccess;
        if (joinChannelSuccessFun != null) {
          joinChannelSuccessFun(channel, uid, elapsed);
        }
      },
      leaveChannel: (stats) {
        _clearUsers();
        var leaveChannelFun = agoraEventHandlers?.leaveChannel;
        if (leaveChannelFun != null) leaveChannelFun(stats);
      },
      userJoined: (uid, elapsed) {
        final info = 'userJoined: $uid';
        print(info);
        _addUser(
          callUser: AgoraUser(
            uid: uid,
          ),
        );
        var userJoinedFun = agoraEventHandlers?.userJoined;
        if (userJoinedFun != null) userJoinedFun(uid, elapsed);
      },
      userOffline: (uid, reason) {
        final info = 'userOffline: $uid , reason: $reason';
        print(info);
        _checkForMaxUser(uid: uid);
        _removeUser(uid: uid);
        var userOfflineFun = agoraEventHandlers?.userOffline;
        if (userOfflineFun != null) userOfflineFun(uid, reason);
      },
      tokenPrivilegeWillExpire: (token) async {
        await _getToken(
          tokenUrl: value.connectionData!.tokenUrl,
          channelName: value.connectionData!.channelName,
          uid: value.connectionData!.uid,
        );
        await value.engine?.renewToken(token);
        var tokenPrivilegeWillExpireFun = agoraEventHandlers?.tokenPrivilegeWillExpire;
        if (tokenPrivilegeWillExpireFun != null) {
          tokenPrivilegeWillExpireFun(token);
        }
      },
      remoteVideoStateChanged: (uid, state, reason, elapsed) {
        final String info = "Remote video state changed for $uid, state: $state and reason: $reason";
        print(info);
        if (uid != value.localUid) {
          if (state == VideoRemoteState.Stopped) {
            _updateUserVideo(uid: uid, videoDisabled: true);
          } else if (state == VideoRemoteState.Decoding && reason == VideoRemoteStateReason.RemoteUnmuted) {
            _updateUserVideo(uid: uid, videoDisabled: false);
          }
        }
        var remoteVideoStateChangedFun = agoraEventHandlers?.remoteVideoStateChanged;
        if (remoteVideoStateChangedFun != null) {
          remoteVideoStateChangedFun(uid, state, reason, elapsed);
        }
      },
      remoteAudioStateChanged: (uid, state, reason, elapsed) {
        final String info = "Remote audio state changed for $uid, state: $state and reason: $reason";
        print(info);
        if (state == AudioRemoteState.Stopped && reason == AudioRemoteStateReason.RemoteMuted) {
          _updateUserAudio(uid: uid, muted: true);
        } else if (state == AudioRemoteState.Decoding && reason == AudioRemoteStateReason.RemoteUnmuted) {
          _updateUserAudio(uid: uid, muted: false);
        }
        var remoteAudioStateChangedFun = agoraEventHandlers?.remoteAudioStateChanged;
        if (remoteAudioStateChangedFun != null) {
          remoteAudioStateChangedFun(uid, state, reason, elapsed);
        }
      },
      localAudioStateChanged: (state, error) {
        final String info = "Local audio state changed state: $state and error: $error";
        print(info);
        var localAudioStateChangedFun = agoraEventHandlers?.localAudioStateChanged;
        if (localAudioStateChangedFun != null) {
          localAudioStateChangedFun(state, error);
        }
      },
      localVideoStateChanged: (localVideoState, error) {
        final String info = "Local audio state changed state: $localVideoState and error: $error";
        print(info);
        var localVideoStateChangedFun = agoraEventHandlers?.localVideoStateChanged;
        if (localVideoStateChangedFun != null) {
          localVideoStateChangedFun(localVideoState, error);
        }
      },
      activeSpeaker: (uid) {
        final String info = "Active speaker: $uid";
        print(info);
        if (value.isActiveSpeakerDisabled == false) {
          final int index = value.users.indexWhere((element) => element.uid == uid);
          swapUser(index: index);
        } else {
          print("Active speaker is disabled");
        }
        var activeSpeakerFun = agoraEventHandlers?.activeSpeaker;
        if (activeSpeakerFun != null) activeSpeakerFun(uid);
      },
    ),
  );
}