1

I am trying to make multiple Controller Types foo multiple Models for use with Provider in Flutter.

This is my Code:

//generic controller
class ModelController<E extends HiveObject> extends ChangeNotifier {
  Box<E> _db;

  ModelController(Box<E> db) {
    _db = db;
  }

  // getters
  List<E> get all => _db.values;

  // create
  void add(E item) {
    _db.add(item);
    notifyListeners();
  }

  void update(E item) {
    item.save();
    notifyListeners();
  }

  // delete
  void delete(E item) {
    item.delete();
    notifyListeners();
  }
}

class PacketController extends ModelController<Packet>{
  PacketController(Box<Packet> db) : super(db);

}

The code for class PacketController is the only way I found to create a concrete type from a generic one.

Question:Is there a better way for this?

Shortly, I am looking for something like typedef in c++:

typedef IntVector std::vector<int>
Bojan Hrnkas
  • 1,587
  • 16
  • 22
  • 2
    Dart (as of version 2.12) supports creating only `typedef`s to functions. However, the upcoming Dart 2.13 release will add support for general `typedef`s as an opt-in, experimental feature. See https://stackoverflow.com/questions/66847006/ – jamesdlin Mar 30 '21 at 20:53
  • What are you trying to do that requires this? For example, what are you using `var controller = PacketController(box);` for that prohibits you from using `var controller = ModelController(box);`? Or is this merely a question about conciseness? – Abion47 Mar 30 '21 at 22:27
  • I find PacketController easier to read than ModelController and I was wondering if it can be defined in one line, instead of creating new Class by extension. – Bojan Hrnkas Mar 31 '21 at 13:39
  • The question is now becoming irrelevant for my project, since I have to implement other methods inside the controller class, but i would still like to find the answer for future reference. – Bojan Hrnkas Mar 31 '21 at 18:53

0 Answers0