0

I'm new to flutter but I'm trying to learn it the right way (Clean Structure) so that I can reuse my code later, although I'm a Programmer for 7 years and I know the concepts.

the App is using these packages (GetX & ObjectBox), the issue is that I can't make them work together in my below app structure:

lib

    app
    ├───database
    │   ├───helpers
    │   └───models
    ├───features
    │   ├───dashboard
    │   │   ├───bindings
    │   │   ├───controllers
    │   │   └───views
    │   │       ├───components
    │   │       └───screens
    │   ├───products
    │   │   ├───bindings
    │   │   ├───controllers
    │   │   └───views
    │   │       ├───components
    │   │       └───screens
    │   ├───other features ...

the workflow for Products would be simply:

  1. create Product class model under app/database/models/product.dart
  2. create ObjectBox class to manage Store under app/database/helpers/objectbox.dart
  3. create ProductHelper class under app/database/helpers/product.dart with other Entities classes to handle CRUD operation for each Entity
  4. create ProductController to interact between ProductHelper and ProductScreen under app/features/products/ and Bindings ofcourse
  5. finally pass data from controller to screen by GetX controller

Files below

Product model class:

part of app_models;

const String productTable = 'product';

class ProductFields {
  static const String pk = 'pk';
  static const String name = 'name';
  static const String createdAt = 'create_at';
  static const String updatedAt = 'updated_at';
}

@Entity()
class Product {
  @Id()
  int pk;
  final String name;
  final DateTime createdAt;
  final DateTime? updatedAt;


  Product({
    this.pk = 0,
    required this.name,
    required this.createdAt,
    this.updatedAt,
  });

  factory Product.fromJson(Map<String, dynamic> json) {
    return Product(
      pk: json['pk'] as int,
      name: json['name'] as String,
      createdAt: DateTime.parse(json['created_at'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String),
    );
  }

  Map<String, dynamic> toJson() => {
        'pk': pk,
        'name': name,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt?.toIso8601String(),
      };
}

ObjectBox class:

class ObjectBox {
  late final Store _store;
  late final Box<Product> _boxProducts;

  ObjectBox(this._store) {
    _boxProducts = Box<Product>(_store);
  }

  static Future<ObjectBox> init() async {
    final store = await openStore();
    return ObjectBox(store);
  }
}

ProductHelper class:

class ProductHelper extends ObjectBox {

  ProductHelper(super.store) ;

  Product? getProduct(int pk) => _boxProduct.get(pk);
  Stream<List<Product>> getProducts() => _boxProduct
      .query()
      .watch(triggerImmediately: true)
      .map((query) => query.find());
  int insertProduct(Product branch) => _boxProduct.put(branch);
  bool deleteProduct(int pk) => _boxProduct.remove(pk);
}

stuck at this level so far in controller can't use methods i.e. insertProduct() from ProductHelper class.

Advice from experts for the work flow and Structure of app files and clean code will be highly appreciated and useful for me and others.

Poula Adel
  • 609
  • 1
  • 10
  • 33
  • 1
    ProductHelper._privateConstructor(); static final ProductHelper instance = ProductHelper._privateConstructor(); Try to declare both line of code in your ProductHelper call. You now can call the function insertProduct from any page like this ProductHelper.instance. insertProduct(xxx); And i would definitely suggest that you have a look at the getx pub dev https://pub.dev/packages/get for more info – HeIsDying Oct 01 '22 at 09:16

0 Answers0