Some examples for how to build dart code using package:analyzer and package:code_builder only (i.e. no build_runner or build.yaml files)

Overview

What is this?

Some examples for how to build dart code from another dart code using package:analyzer and package:code_builder only (i.e. no build_runner or build.yaml files).

In the example folder, there are three different stand-alone examples. Each one of them can be simply executed as follow:

[~/path/to/repo] dart run example/<file_name>.dart
  • ast_example
    • Shows how an Abstract Syntax Tree (AST) can be generated from a source code using package:analyzer.
    • How to visit elements in the tree.

  • code_builder
    • Shows how to build code programmatically using package:code_builder

  • data_class_builder_example

    • combines both examples to generate a data class.

    • In short, it takes this as an input:

    class Person  { 
        final String name;
        final String? nickname;
        final int age;
        final double height;
        final List<String> hobbies;
    }
    • To generate this as an output:

    class Person {
        const Person({
            required this.name,
            this.nickname,
            required this.age,
            required this.height,
            required this.hobbies,
        });
    
        factory Person.fromMap(Map<String, dynamic> map) {
            return Person(
            name: map['name'],
            nickname: map['nickname'],
            age: map['age'].toInt(),
            height: map['height'].toDouble(),
            hobbies: List.from(map['hobbies']),
            );
        }
    
        factory Person.fromJson(String source) => Person.fromMap(json.decode(source));
    
        final String name;
    
        final String? nickname;
    
        final int age;
    
        final double height;
    
        final List<String> hobbies;
    
        Person copyWith({
            String? name,
            String? nickname,
            int? age,
            double? height,
            List<String>? hobbies,
        }) {
            return Person(
            name: name ?? this.name,
            nickname: nickname ?? this.nickname,
            age: age ?? this.age,
            height: height ?? this.height,
            hobbies: hobbies ?? this.hobbies,
            );
        }
    
        String toJson() => json.encode(toMap());
        Map<String, dynamic> toMap() {
            return {
            'name': name,
            'nickname': nickname,
            'age': age,
            'height': height,
            'hobbies': hobbies,
            };
        }
    
        @override
        String toString() {
            return 'Person(name: $name, nickname: $nickname, age: $age, height: $height, hobbies: $hobbies)';
        }
    
        @override
        bool operator ==(Object other) {
            if (identical(this, other)) return true;
            // TODO: handle list equality here
            return other is Person &&
                other.name == name &&
                other.nickname == nickname &&
                other.age == age &&
                other.height == height &&
                other.hobbies == hobbies;
        }
    
        @override
        int get hashCode {
            return name.hashCode ^ nickname.hashCode ^ age.hashCode ^ height.hashCode ^ hobbies.hashCode;
            }
    }
    

Disclimar: These examples were written (poorly) for learning purposes and to understand how these packages work together -- no more no less.

You might also like...

A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

Oct 26, 2022

VS Code `.code-workspace` file generator

VS Code .code-workspace file generator (for monorepositories with Dart and Flutter projects) TL;DR; Create yaml file config.yaml (check #Format sectio

Feb 18, 2022

A cli tool to run Flutter applications and auto hot reload it when files are changed

Dashmon A minimalistic CLI tool to run Flutter applications and auto hot reload it when files are changed. It will watch changes your application code

Oct 6, 2022

A set of commands for coverage info files manipulation.

Coverage Utils A set of commands for coverage info files manipulation. Installing $ dart pub global activate

Oct 9, 2022

A generator to create config class from json files that support many environments

A generator to create config class from json files that support many environments. Motivation If you use a json file to config your applications, perp

Oct 9, 2021

A CLI tool to help batch renaming files.

batch_rename A CLI tool to enable batch renaming of files. Installation Clone the repo and add bin/batch_rename.exe to PATH: gh repo clone POWRFULCOW8

Nov 3, 2021

A dart package for decode and encode emv QR code

A dart package for decode and encode emv QR code

Nov 15, 2022

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. There is no way to get the package version from the code in the Dart ecosystem. Installation Add bu

Dec 14, 2022
Owner
null
Dart R-file generator for build_runner

r_resources This package is made for R-file code generation using build_runner.

Ivanov Nikita 0 Dec 17, 2021
A very basic prototype of macros using build_runner

Description This is a basic prototype for 3 phase macros using package:build. The general idea is that macros run in 3 different phases, and each phas

Jacob MacDonald 64 Dec 14, 2022
A library for YAML manipulation with comment and whitespace preservation.

Yaml Editor A library for YAML manipulation while preserving comments. Usage A simple usage example: import 'package:yaml_edit/yaml_edit.dart'; void

Dart 17 Dec 26, 2022
Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.

Google 318 Dec 31, 2022
Examples of Flutter Code Generation.

Flutter Code Generation Examples Code generation packages/tools used in the app: flutter_localization/intl build_runner flutter_gen/flutter_gen_runner

Mangirdas Kazlauskas 19 Oct 11, 2022
A Dart testing utility for asserting that some code emits a compilation error.

A Dart testing utility for asserting that some code emits a compilation error.

Remi Rousselet 32 Dec 11, 2022
Library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script

To-Do telegram client dart ✅️ support multi token ( bot / userbot ) ✅️ support bot and userbot ✅️ support telegram-bot-api local server ✅️ support tel

Azka Full Snack Developer:) 73 Jan 7, 2023
Minimal Dart wrapper to interact with Some Random Api. Easy to use, simplified and lightweight.

SRA - Some Random Api Minimal Dart wrapper to interact with Some Random Api. Easy to use, simplified and lightweight. Getting started Add the package

Yakiyo 3 Jan 4, 2023
Flutter package to parse iCalendar (.ics) files.

icalendar_parser Package to parse iCalendar (.ics) files written in pure Dart. Implementation of AnyFetch's ics-parser in JavaScript. Getting Started

Guillaume Roux 27 Jan 3, 2023
A Dart package to web scraping data from websites easily and faster using less code lines.

Chaleno A flutter package to webscraping data from websites This package contains a set of high-level functions that make it easy to webscrap websites

António Nicolau 30 Dec 29, 2022