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

  1. 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 }
      

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.

  1. 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
      
  2. 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 from recipe.mapper.dart.

  3. 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.