Skip to content

judge0/judge0-dart

Repository files navigation

Judge0 Dart SDK

The official Dart SDK for Judge0.

import 'package:judge0/judge0.dart';

void main() async {
  final result = await run(sourceCode: "print('hello, world')");
  print(result.stdout); // 'hello, world\n'
  print(result.time); // 0.987
  print(result.memory); // 52440
  
  for (final file in result.filesystem) {
    print(file.name); // 'script.py'
    print(file.stringContent); // "print('hello, world')"
  }
}

Requirements

  • Dart 3.0+

Quick Start

Getting The API Key

Get your API key from Sulu, Rapid, or ATD.

Notes

  • [Recommended] Choose Sulu if you need pay-as-you-go (pay-per-use) pricing.
  • Choose Rapid or ATD if you need a quota-based (volume-based) plan.
  • Judge0 has two flavors: Judge0 CE and Judge0 Extra CE, and their difference is just in the languages they support. When choosing Sulu, your API key will work for both flavors, but for Rapid and ATD you will need to explicitly subscribe to both flavors if you want to use both.

Using Your API Key

Option 1: Explicit Client Object

Explicitly create a client object with your API key and pass it to Judge0 Dart SDK functions.

import 'package:judge0/judge0.dart';

void main() async {
  final client = SuluJudge0CE(apiKey: "xxx");
  final result = await run(client: client, sourceCode: "print('hello, world')");
  print(result.stdout);
}

Other options include:

  • RapidJudge0CE
  • ATDJudge0CE
  • SuluJudge0ExtraCE
  • RapidJudge0ExtraCE
  • ATDJudge0ExtraCE

Option 2: Implicit Client Object

Put your API key in one of the following environment variables, respective to the provider that issued you the API key: JUDGE0_SULU_API_KEY, JUDGE0_RAPID_API_KEY, or JUDGE0_ATD_API_KEY.

Judge0 Dart SDK will automatically detect the environment variable and use it to create a client object that will be used for all API calls if you do not explicitly pass a client object.

import 'package:judge0/judge0.dart';

void main() async {
  final result = await run(sourceCode: "print('hello, world')");
  print(result.stdout);
}

Examples

Running C Programming Language

import 'package:judge0/judge0.dart';

void main() async {
  const sourceCode = """
#include <stdio.h>

int main() {
    printf("hello, world\\n");
    return 0;
}
""";

  final result = await run(
    sourceCode: sourceCode,
    kwargs: {'language': LanguageAlias.c},
  );
  print(result.stdout);
}

Running Java Programming Language

import 'package:judge0/judge0.dart';

void main() async {
  const sourceCode = """
public class Main {
    public static void main(String[] args) {
        System.out.println("hello, world");
    }
}
""";

  final result = await run(
    sourceCode: sourceCode,
    kwargs: {'language': LanguageAlias.java},
  );
  print(result.stdout);
}

Reading From Standard Input

import 'package:judge0/judge0.dart';

void main() async {
  const sourceCode = """
#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d\\n", a + b);

    char name[10];
    scanf("%s", name);
    printf("Hello, %s!\\n", name);

    return 0;
}
""";

  const stdin = """
3 5
Bob
""";

  final result = await run(
    sourceCode: sourceCode,
    kwargs: {'stdin': stdin, 'language': LanguageAlias.c},
  );
  print(result.stdout);
}

Test Cases

import 'package:judge0/judge0.dart';

void main() async {
  final results = await run(
    sourceCode: "print(f'Hello, {input()}!')",
    testCases: [
      ["Bob", "Hello, Bob!"], // Test Case #1. List with first value as standard input, second value as expected output.
      { // Test Case #2. Map with "input" and "expected_output" keys.
        "input": "Alice",
        "expected_output": "Hello, Alice!"
      },
      ["Charlie", "Hello, Charlie!"], // Test Case #3. List with first value as standard input and second value as expected output.
    ],
    kwargs: {'language': LanguageAlias.python},
  );

  for (int i = 0; i < results.length; i++) {
    print("--- Test Case #${i + 1} ---");
    print(results[i].stdout);
    print(results[i].status);
  }
}

Test Cases And Multiple Languages

import 'package:judge0/judge0.dart';

void main() async {
  final submissions = [
    Submission(
      sourceCode: "print(f'Hello, {input()}!')",
      language: LanguageAlias.python,
    ),
    Submission(
      sourceCode: """
#include <stdio.h>

int main() {
    char name[10];
    scanf("%s", name);
    printf("Hello, %s!\\n", name);
    return 0;
}
""",
      language: LanguageAlias.c,
    ),
  ];

  const testCases = [
    ["Bob", "Hello, Bob!"],
    ["Alice", "Hello, Alice!"],
    ["Charlie", "Hello, Charlie!"],
  ];

  final results = await run(
    submissions: submissions,
    testCases: testCases,
  );

  for (int i = 0; i < submissions.length; i++) {
    print("--- Submission #${i + 1} ---");

    for (int j = 0; j < testCases.length; j++) {
      final result = results[i * testCases.length + j];

      print("--- Test Case #${j + 1} ---");
      print(result.stdout);
      print(result.status);
    }
  }
}

Asynchronous Execution

import 'package:judge0/judge0.dart';

void main() async {
  final submission = await asyncExecute(
    sourceCode: "print('hello, world')",
    kwargs: {'language': LanguageAlias.python},
  );
  print(submission.token); // Prints the submission token
  print(submission.status); // Prints 'inQueue' or 'processing'

  await wait(submissions: submission); // Wait for the submission to finish.

  print(submission.stdout); // Prints 'hello, world'
  print(submission.status); // Prints 'accepted'
}

Get Languages

import 'package:judge0/judge0.dart';

void main() async {
  final client = getClient();
  final languages = await client.getLanguages();
  print(languages);
}

Development

Setting Up VS Code

  1. Install Dart SDK:

    # Install Dart SDK (if not already installed)
    brew install dart  # macOS
    # or download from https://dart.dev/get-dart
  2. Install VS Code Extensions:

    • Dart - Official Dart language support
    • Flutter - For Flutter development (if needed)

Running the Project

1. Basic Setup

# Clone the repository
git clone <repository-url>
cd judge0

# Install dependencies
dart pub get

# Run code generation (if needed)
dart run build_runner build

2. Running Examples

# Run individual examples
dart example/0001_hello_world.dart
dart example/0002_hello_world.dart
dart example/0003_hello_world.dart

# Run all examples
dart example/run_all_examples.dart

Testing

Unit Tests

# Run all tests
dart test

# Run tests with verbose output
dart test --verbose

# Run tests matching a pattern
dart test --name="async"

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

The official Dart SDK for Judge0.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages