Supabase has type generators for other languages... but not for Dart / Flutter
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;
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.