Supabase has type generators for other languages... but not for Dart / Flutter
Supadart automatically generates Dart classes based on your Supabase schema, providing you with a typesafe way to interact with your database.
You can generate via CLI or in the Official Website
Assuming you have the following Supabase Schema
create table
public.books (
id bigint generated by default as identity,
name character varying not null,
description text null,
price integer not null,
created_at timestamp with time zone not null default now(),
constraint books_pkey primary key (id)
) tablespace pg_default;
Then it will generate these classes
class Books implements SupadartClass<Books> {
final BigInt id;
final String name;
final String? description;
final int price;
final DateTime? createdAt;
const Books({
required this.id,
required this.name,
this.description,
required this.price,
this.createdAt,
});
static String get table_name => 'books';
static String get c_id => 'id';
static String get c_name => 'name';
static String get c_description => 'description';
static String get c_price => 'price';
static String get c_createdAt => 'created_at';
static List<Books> converter(List<Map<String, dynamic>> data) {
return data.map(Books.fromJson).toList();
}
static Books converterSingle(Map<String, dynamic> data) {
return Books.fromJson(data);
}
static Map<String, dynamic> _generateMap({
BigInt? id,
String? name,
String? description,
int? price,
DateTime? createdAt,
}) {
return {
if (id != null) 'id': id.toString(),
if (name != null) 'name': name,
if (description != null) 'description': description,
if (price != null) 'price': price,
if (createdAt != null) 'created_at': createdAt.toUtc().toIso8601String(),
};
}
static Map<String, dynamic> insert({
BigInt? id,
required String name,
String? description,
required int price,
DateTime? createdAt,
}) {
return _generateMap(
id: id,
name: name,
description: description,
price: price,
createdAt: createdAt,
);
}
static Map<String, dynamic> update({
BigInt? id,
String? name,
String? description,
int? price,
DateTime? createdAt,
}) {
return _generateMap(
id: id,
name: name,
description: description,
price: price,
createdAt: createdAt,
);
}
factory Books.fromJson(Map<String, dynamic> jsonn) {
return Books(
id: jsonn['id'] != null
? BigInt.parse(jsonn['id'].toString())
: BigInt.from(0),
name: jsonn['name'] != null ? jsonn['name'].toString() : '',
description:
jsonn['description'] != null ? jsonn['description'].toString() : '',
price: jsonn['price'] != null ? int.parse(jsonn['price'].toString()) : 0,
createdAt: jsonn['created_at'] != null
? DateTime.parse(jsonn['created_at'].toString())
: DateTime.fromMillisecondsSinceEpoch(0),
);
}
Map<String, dynamic> toJson() {
return _generateMap(
id: id,
name: name,
description: description,
price: price,
createdAt: createdAt,
);
}
}
This makes it easy to interact with your database, and you can use the generated classes in your code.
mmvergara / supadart
Typesafe queries in Supabase Flutter! Generate Flutter / Dart ๐ฏ classes from your Supabase schema.
Supadart ๐ฏ
Typesafe Supabase Flutter Queries
Generate Flutter / Dart ๐ฏ classes from your Supabase schema.
// allBooks is a typeof List<Books>
final allBooks = await supabase
.books
.select("*")
.withConverter(Books.converter);
Table of Contents ๐
Features ๐
- ๐ Cli and Web App
- ๐ ๏ธ Typesafe Queries (Create, Read, Equality)
- ๐งฑ Immutable Generated Classes
- ๐๏ธ Roundtrip Serialization fromJson to toJson and back
- ๐ Supports Column Selection Queries
- ๐ข Supports all Supabase Major datatypes
- ๐๏ธ Supports Defined as array types
- ๐๏ธ Supports Enums
Top comments (0)