Skip to content
Efra Espada edited this page Feb 22, 2024 · 3 revisions

Add Dependency

Include firebase_cloud_firestore as a dependency in your pubspec.yaml file and configure it accordingly:

dependencies:
  firebase_cloud_firestore: ^4.15.4

object:
  baseProjectFolder: 'lib'
  outputFolder: 'model/generated'
  modelsFile: 'model.g.dart'
  generationClassSuffix: 'Gen'

Configure Classes

Before utilizing the object library, ensure to configure the classes that you'll retrieve from Firestore. Here's an example:

import 'generated/model.g.dart';

class Chat extends ChatGen {
  @override
  @Field(
    name: 'id',
    primary: true,
  )
  String id = '';

  @override
  @Field(name: 'name')
  String name = '';

  @override
  @Field(name: 'multiple')
  bool multiple = false;

  @override
  @Field(name: 'members')
  List<String> members = [];

  @override
  @Field(name: 'creation')
  DateTime? creation = null;

  Chat();
}

Generate Code

Run the following command to generate the necessary code:

dart run object:build

Configure Application

Integrate the generated code into your application:

import 'package:flutter/material.dart';
import 'package:object/object.dart';

import 'model/sample.dart';

void main() {
  /// From your pubspec.yaml configuration:
  /// 
  /// modelsFile: 'model.g.dart'
  /// 
  /// generates the [Model] class:
  Model().instancesForLoad();
  runApp(const MyApp());
}

Go to object documentation for more information.

Clone this wiki locally