Defining your data types
With Celest Functions, you can use any of the core Dart types available such as int
, String
, and DateTime
.
You can also use your own data types. Celest will automatically handle the transfer of data to and from your Flutter app
(a process known as serialization) so you don't need to think about it.
If you'd prefer to handle the serialization yourself, Celest will use your custom logic instead.
Imagine you're working on an e-commerce application with an Order
class defined in your codebase.
class Order {
const Order ({
required this.id,
required this.customerName,
required this.price,
});
final int id;
final String customerName;
final Price price;
}
enum Currency { usd, cad, ... }
class Price {
const Price({
required this.currency,
required this.dollars,
required this.cents,
}): assert(cents < 100);
final Currency currency;
final int dollars;
final int cents;
}
When you pass this Order
type as a parameter to a cloud function or when you return it from a cloud function,
Celest will automatically serialize and deserialize it for you.
import 'package:celest_backend/models/order.dart';
@cloud
Future<String> createOrder(
Order customerOrder,
) async {
// Function logic goes here
}
Celest does this by inspecting every function marked with @cloud
to find the data types you've used and any types those types use
and generates a schema for each of them. This schema is then used to serialize and deserialize the data to and from JSON.
For example when passing an order to your backend, Celest will serialize the Order
class as a JSON map with the field names as keys and
the values marshalled to the appropriate JSON representation.
{
"customerOrder": {
"id": 123,
"customerName": "Celest",
"price": {
"currency": "usd",
"dollars": 100,
"cents": 34
}
}
}
Similarly if your Flutter app receives an Order
object from a Celest Function, Celest knows how to take this JSON and map it back into
the same Dart type. The process happens so transparently that you'll feel like you're just passing Dart objects directly between two
local functions.
Next steps
Read on to see how you can seamlessly catch errors thrown from your backend in your Flutter app and how to configure the environment in which your backend runs.