Dart Language Samples

repository·main·Indexed 20 days ago

https://github.com/dart-lang/samples

A collection of Dart programs illustrating language features, best practices, and implementation patterns. Examples cover FFI (Foreign Function Interface), Isolates, Null Safety, enhanced enums (Dart 2.17+), extension methods (Dart 2.6.0+), super-initializer parameters, and native application compilation via AOT snapshots and standalone executables.

Tokens
9.8K
Snippets
48
Records
59
Agent score
70%

What's inside dart-lang-samples

  1. Explore function parameters and super-initializer samples

    main

    This sample repository demonstrates advanced Dart language features introduced in Dart 2.17. It provides practical examples of:

    • Function parameters: Including named parameters, optional parameters, and the ability to position named arguments anywhere in an argument list.
    • Super-initializer parameters: Demonstrating how to pass parameters directly to a superclass constructor.

    Requirement: These samples require Dart 2.17 or later. They will not compile on older versions of the Dart SDK.

  2. Explore Dart feature samples

    main
    This repository contains a collection of Dart programs designed to illustrate specific language features and best practices. You can use these samples to learn how to implement advanced Dart concepts in your own projects.
  3. Explore enhanced enums samples

    main

    This repository contains a collection of Dart samples demonstrating the enhanced enums syntax introduced in Dart 2.17. These samples cover various use cases, including adding functionality via mixins and providing a comprehensive implementation of all enhanced enum features.

    Note: These programs require Dart 2.17 or later and will not compile in earlier versions.

  4. Solve Constraint Satisfaction Problems (CSP) with backtrackingSearch

    main

    To solve a problem using the constraint_solver package, you must model it as a Constraint Satisfaction Problem (CSP) consisting of three components:

    1. Variables: A list of entities that need to be assigned values.
    2. Domains: A mapping where each variable is associated with a list of possible values.
    3. Constraints: Rules that must be satisfied by the assignments. You implement these by subclassing the Constraint<V, D> class and overriding the isSatisfied(Map<V, D> assignment) method.

    Once modeled, instantiate the CSP<V, D> class with your variables and domains, add your constraints using addConstraint(), and execute the solver using backtrackingSearch().

    // 1. Define variables
    var doug = Person('Doug', dislikes: ['artichoke']);
    var patrick = Person('Patrick', dislikes: ['bananas']);
    var susan = Person('Susan', dislikes: ['broccoli']);
    var variables = [doug, patrick, susan];
    
    // 2. Define domains
    var meals = ['artichoke', 'bananas', 'broccoli'];
    var domains = {
      doug: meals,
      patrick: meals,
      susan: meals,
    };
    
    // 3. Define constraints by subclassing Constraint
    class AvoidDislikes extends Constraint<Person, String> {
      AvoidDislikes(super.variables);
    
      @override
      bool isSatisfied(Map<Person, String> assignment) {
        // Implementation logic goes here
        return true;
      }
    }
    
    // 4. Run the solver
    var csp = CSP<Person, String>(variables, domains);
    csp.addConstraint(AvoidDislikes(variables));
    
    var result = csp.backtrackingSearch();
    print(result);
  5. Build native libraries for Dart FFI samples

    main

    Each sample in this repository uses CMake to generate a Makefile. To build the native C library required for a sample, navigate to the library directory, run cmake ., and then execute make. This will produce a shared library file: libhello.dylib on macOS, libhello.dll on Windows, or libhello.so on Linux.

    cd hello_world/hello_library
    cmake .
    make
  6. Run the calculate_lix CLI app

    main

    The calculate_lix sample is a CLI application that calculates the 'lix' readability index for a text file. It is designed to demonstrate Dart's null safety features in a practical context.

    Prerequisites

    • Dart SDK 2.12 or later
    • (Optional) Flutter 2.0 or later if using Flutter

    Execution Steps

    1. Navigate to the sample directory.
    2. Fetch dependencies using dart pub get.
    3. Run the application using dart run bin/main.dart followed by the path to a text file.
    $ cd <folder with samples repo>/null_safety/calculate_lix/
    $ dart pub get
    $ dart run bin/main.dart text/lorem-ipsum.txt
  7. Build and run an AOT snapshot

    main

    To create an Ahead-of-Time (AOT) snapshot, use the dart compile aot-snapshot command with the --output-kind or -k flag. Unlike a standalone executable, an AOT snapshot must be executed using the dartaotruntime command.

    # Build the snapshot
    dart compile aot-snapshot bin/main.dart -o hello_world.aot
    
    # Run the snapshot
    dartaotruntime hello_world.aot
  8. Run Dart FFI samples

    main
    After successfully building the native library, you must resolve Dart dependencies and then execute the Dart entry point. Use dart pub get to fetch dependencies followed by dart run <filename>.dart to execute the sample.
    dart pub get
    dart run <filename>.dart