fromMap static method

AppwinCommunityEvent? fromMap(
  1. Map<Object?, Object?>? map
)

Parses what the native side sends over the event channel.

null for an event type this package does not know, rather than a throw: a native SDK newer than this package may report more kinds of events, and those must not break the host app.

Implementation

static AppwinCommunityEvent? fromMap(Map<Object?, Object?>? map) {
  String? field(String key) {
    final value = map?[key];
    return value is String ? value : null;
  }

  final postId = field('postId');
  final commentId = field('commentId');
  switch (map?['type']) {
    case 'postCreated' when postId != null:
      return AppwinCommunityPostCreated(postId: postId);
    case 'commentCreated' when postId != null && commentId != null:
      return AppwinCommunityCommentCreated(
        commentId: commentId,
        postId: postId,
      );
    case 'replyCreated' when postId != null && commentId != null:
      final replyId = field('replyId');
      if (replyId == null) return null;
      return AppwinCommunityReplyCreated(
        replyId: replyId,
        commentId: commentId,
        postId: postId,
      );
    case 'reactionModified' when postId != null:
      return AppwinCommunityReactionModified(
        postId: postId,
        commentId: commentId,
        reaction: field('reaction'),
      );
    case 'profileUpdated':
      final profileId = field('profileId');
      if (profileId == null) return null;
      return AppwinCommunityProfileUpdated(profileId: profileId);
    default:
      return null;
  }
}