Repository: KRTirtho/spotube
File: lib/services/sourced_track/sources/youtube.dart (Dart)

Code before:
    if (ServiceUtils.onlyContainsEnglish(query)) {
      siblings.addAll(await Future.wait(searchResults
          .map(YoutubeVideoInfo.fromVideo)
          .mapIndexed((index, info) => toSiblingType(index, info, ref))));
    } else {
      final rankedSiblings = rankResults(
        searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
        track,
      );
      siblings.addAll(await Future.wait(
        rankedSiblings
            .mapIndexed((index, info) => toSiblingType(index, info, ref)),
      ));
    }
    return await (() async {
      // Deduplicate siblings by info.id, keeping the first occurrence
      final seenIds = <String>{};
      final uniqueSiblings = <SiblingType>[];
      for (final sibling in siblings) {
        if (!seenIds.contains(sibling.info.id)) {
          seenIds.add(sibling.info.id);
          uniqueSiblings.add(sibling);
        }
      }
      return uniqueSiblings;
    })();
  }

  @override
  Future<YoutubeSourcedTrack?> swapWithSibling(SourceInfo sibling) async {
    if (sibling.id == sourceInfo.id) {
      return null;
    }

    // a sibling source that was fetched from the search results
    final isStepSibling = siblings.none((s) => s.id == sibling.id);

    final newSourceInfo = isStepSibling
        ? sibling
        : siblings.firstWhere((s) => s.id == sibling.id);
    final newSiblings = siblings.where((s) => s.id != sibling.id).toList()
      ..insert(0, sourceInfo);

    final manifest = await ref
        .read(youtubeEngineProvider)
        .getStreamManifest(newSourceInfo.id);

    final database = ref.read(databaseProvider);

    await database.into(database.sourceMatchTable).insert(
          SourceMatchTableCompanion.insert(

Review comment (suggestion):
Is the self executing unnamed function necessary? You can just do that on the main scope. Putting a computation related task on an async block won't make the computation run in parallel or not block the main thread 

Code after:
    if (ServiceUtils.onlyContainsEnglish(query)) {
      videoResults.addAll(
          searchResults.map(YoutubeVideoInfo.fromVideo).toList()
      );
    } else {
      videoResults.addAll(rankResults(
          searchResults.map(YoutubeVideoInfo.fromVideo).toList(),
          track,
      ));
    }

    final seenIds = <String>{};
    int index = 0;
    return await Future.wait(
      videoResults.map((videoResult) async {
        // Deduplicate results
        if (!seenIds.contains(videoResult.id)) {
          seenIds.add(videoResult.id);
          return await toSiblingType(index++, videoResult, ref);
        }
        return null;
      }),
    ).then((s) => s.whereType<SiblingType>().toList());
  }

  @override
  Future<YoutubeSourcedTrack?> swapWithSibling(SourceInfo sibling) async {
    if (sibling.id == sourceInfo.id) {
      return null;
    }

    // a sibling source that was fetched from the search results
    final isStepSibling = siblings.none((s) => s.id == sibling.id);

    final newSourceInfo = isStepSibling
        ? sibling
        : siblings.firstWhere((s) => s.id == sibling.id);
    final newSiblings = siblings.where((s) => s.id != sibling.id).toList()
      ..insert(0, sourceInfo);

    final manifest = await ref
        .read(youtubeEngineProvider)
        .getStreamManifest(newSourceInfo.id);

    final database = ref.read(databaseProvider);

    await database.into(database.sourceMatchTable).insert(
          SourceMatchTableCompanion.insert(
            trackId: id!,
            sourceId: newSourceInfo.id,
            sourceType: const Value(SourceType.youtube),