Quick Start Guide for Dart Package Experts
This section provides an overview of the Quick Start Guide for Dart package experts.
Follow these simple instructions to set up your project efficiently and begin creating content without delay!:
Steps
- Define Your Model
- Let's create a
recipe.dart
file and add your model:// We need [dart_mappable] to generate the mappers import 'package:dart_mappable/dart_mappable.dart'; // [recipe.mapper.dart] will be the file generated part 'recipe.mapper.dart'; () class Recipe with RecipeMappable { final String persistentModelID; // This field is required for the cozy_data package to work String name; List<Ingredients>? ingredients; Recipe({required this.id, required this.name, this.ingredients}); } () class Ingredients with IngredientsMappable { String name; int? quantity; CookStyle cookStyle; Ingredients({required this.name, this.quantity, required this.cookStyle}); } () enum CookStyle { bake, fry, boil }
- Let's create a
For comprehensive class annotation details, explore the dart_mappable packageβan absolutely brilliant toolkit that developers will adore! π Its elegant design and robust functionality make it a true gem in the world of Dart development.
-
Generate the Mapper
- Run
dart run build_runner build
to generate your model mapper. You will find it in the structure like this:root βββ <folder>/ β βββ recipe.dart β βββ recipe.mapper.dart
- Run
-
Initialize CozyData
- Initialize CozyData in your
main.dart
:void main() async { WidgetsFlutterBinding.ensureInitialized(); await CozyData.initialize( engine: CozyEngine.sqlite3, mappers: [RecipeMapper.ensureInitialized()]); runApp(MyApp()); }
Info:
The
RecipeMapper
came fromrecipe.mapper.dart
. - Initialize CozyData in your
-
Simple Recipe Query Data with UI Updates
class RecipeListView extends StatefulWidget { _RecipeListViewState createState() => _RecipeListViewState(); } class _RecipeListViewState extends State<RecipeListView> { final CozyQueryListener<Recipe> _recipeQuery = CozyData.queryListener<Recipe>(); Widget build(BuildContext context) { return Scaffold( body: ListenableBuilder( listenable: _recipeQuery, builder: (context, _) { final recipes = _recipeQuery.items; return ListView.builder( itemCount: recipes.length, itemBuilder: (context, index) => ListTile( title: Text(recipes[index].name), ), ); }, ), floatingActionButton: FloatingActionButton( onPressed: () async { /// Generate a new id for the recipe final id = CozyId.cozyPersistentModelIDString(); /// Create a new recipe with the generated id final recipe = Recipe( persistentModelID: id, name: "Salad", ingredients: [ Ingredients( name: 'eggs', quantity: 2, cookStyle: CookStyle.boil, ), Ingredients( name: 'Onion', quantity: 1, cookStyle: CookStyle.fry, ), ] ); // Save the recipe to the database // Make sure to use the model <Model> to save in the database await CozyData.save<Recipe>(recipe); }, child: const Icon(Icons.add), ), ); } void dispose() { _recipeQuery.dispose(); super.dispose(); } }
Info:
The annotation await CozyData.save<Recipe>(recipe);
is used to save data, and the <Recipe>
specifies the data model. Make sure to always add the <dataModel>
.
Explore Further
This section serves as your starting point for exploring the package. By following the steps above, you can quickly and easily understand all the advanced features. For more customization and control, please refer to the following pages.