Flutter model generator

Flutter plugin to auto generate Flutter model files to a common standard. An exercise to understand Flutter plugin development whilst additionally reducing the time spent writing and updating flutter model classes.

The goal

Something I learned whilst working on a 2 year long flutter project involving multiple apis and providers whilst additionally resolving custom UI interactions is that writing, updating and fixing models is a very time consuming process. And whilst there are some execellent plugins to build models from graphql schemas, its not always the case that you want to be dependent on these, or may not have a graphql schema to draw from.

Additionally, the power of flutter is being able to write your own builder plugins and as such I wanted to work out how they worked. The results is a non-dependent model builder that leverages graphql schema to auto generate a fairly simple flutter model class with the bare minimum features. Such as toJson(), fromJson() copyWith().

To clarify what I mean by non-dependent, I mean that the code it generates is final, and sort of ok looking, you don't actually need the plugin afterwards. You could just delete it. You don't need it to run your project.

You may find that actually you dont even need to integrated into your project at all, just use it to quick generate models in the exmple folder and cut and paste as required.

Why not LLMs? Definitly use LLMS.

In the back of my mind I'm thinking I really should be using an LLM, its all the rage, and can definitely do the job. In fact I wrote several prompts providing example code outputs to do the job, and it worked just fine.

The problem? Just another dependency to ruin your day. Sure, internet down time is by and large not something that happens, but still, you'll thank yourself when it does go down that you can still run it. At the time I built this, I wasn't aware of any LLMs you could easily run locally on your GPU. ALso, i wan't to understand how to use and programme a flutter builder.

That said, absolutely I want to run this via an LLM as its just the better and quicker way to do this type of job. And as soon as I get the chance I'll be using a local LLM running on my GPU with some third party package to basically programme everything.

The repo

https://github.com/emileswain/graphql_model_generator

The code needs tidying up further, refactoring a little. Which will happen when I work on another project that I use this on. Then I may deploy it to as a public package.

Model example

Below is a pretty standard example of a model generated from the graphql schema

type Author{
    name: String!,
    books:[BookModel]!
}
import 'package:your_app_package/data/models/books.dart';


class Author{
  Author({
    required this.name,
    required this.books,
  });

  factory Author.fromJson(Map<String, dynamic> json) {
    List<ImageModel> newBooks = [];
    final jsonBooks = List<dynamic>.from(json['books']).toList();
    // usually I try catch and am happy with dropping a book if it failes
    // for some reason.
    for (var itemData in jsonBooks ) {
      final item = BookModel.fromJson(itemData);
      if (item != null) {
        newBooks.add(item);
      }
    }
    return Author(
      name: json['name'],
      images: newBooks,
    );
  }

  final String name;
  final List<BookModel> images;

  Map<String, dynamic> toJson() => {
        "name": name,
        "books": books.map((item) => item.toJson()).toList(),
      };

  Author copyWith({
    String? name,
    List<BookModel>? books,
  }) {
    return ImageCollection(
      name: name ?? this.name,
      books: books ?? this.books,
    );
  }
}