firestorm 1.0.1
firestorm: ^1.0.1 copied to clipboard
A data access API and ODM tool for Firebase's Firestore and Realtime Database
/// This example shows basic usage of Firestorm:
/// - Defining a data entity annotated with @FirestormObject (see commented code below).
/// - Generating code using the Firestorm code generator.
/// - Initializing Firestorm with Firebase.
/// - Registering classes
/// - Using Firestorm to perform CRUD operations on the data entity.
/*
import 'package:firestorm/firestorm.dart';
@FirestormObject()
class Person {
String id;
String firstname;
String lastname;
int age;
double height;
bool isEmployed;
List<String> friends;
Person(this.id, this.firstname, this.lastname, this.age, this.height,
this.isEmployed, this.friends);
}
*/
import 'package:firestorm/annotations/exclude.dart';
import 'package:firestorm/annotations/firestorm_object.dart';
import 'package:firestorm/firestorm.dart';
import 'package:firestorm/fs/fs.dart';
import 'package:firestorm/rdb/rdb.dart';
import 'package:flutter/material.dart';
// ############################# SAMPLE DATA CLASS #############################
// Sample data class - typically declared in a separate file.
@FirestormObject() // This annotation is used to mark the class for Firestorm code generation.
class Person {
String id; // Necessary ID field for Firestorm objects.
String firstname;
String lastname;
int age;
@Exclude()
String tempData; // This field will be excluded from Firestorm operations due to the @Exclude() annotation.
int? optionalField; // Example of an optional field (nullable).
int namedRequiredField; // Example of a named required field (non-nullable).
String _privateField; // Example of a private field, which will require a getter and setter for Firestorm to access.
String get privateField => _privateField; // Getter for the private field.
set privateField(String value) => _privateField = value; // Setter for the private field.
Person(
this.id,
this.firstname,
this.lastname,
this.age,
this.tempData,
this._privateField,
{
this.optionalField,
required this.namedRequiredField
}
);
}
// #############################################################################
// ############################### MAIN FUNCTION ###############################
main() async {
WidgetsFlutterBinding.ensureInitialized(); // Initialize Flutter first
await FS.init(); // Initialize Firestore if used.
await RDB.init(); // Initialize Realtime database if used.
// TODO - Uncomment when generated:
//registerClasses(); // To be generated by installing `build_runner` and running `dart pub run build_runner build --delete-conflicting-outputs`
runApp(MyApp()); //Run your Flutter app here...
}
// #############################################################################
// ################################ APP WIDGET #################################
// App widget - typically defined in a separate file.
class MyApp extends StatelessWidget {
// Example object
Person person = Person(
"123",
"John",
"Doe",
30,
"This is temp data",
"This is private data",
optionalField: 42,
namedRequiredField: 99
);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firestorm Example',
home: Scaffold(
appBar: AppBar(title: Text('Firestorm Example')),
body: Center(
child: ElevatedButton(
onPressed: () {
// Example button to trigger Firestorm operation
FS.create.one(person).then((_) {
print("Person created successfully!");
}).catchError((error) {
print("Error creating person: $error");
});
},
child: Text('Create Person'),
),
),
),
);
}
}
// #############################################################################