dart

Simple Data Structures in Dart

void main() {
  //print(_showDateFormat());

  Dao.I.greet();
  Dao.I.incr();
  print(Dao.I.counter);

  var oneTask = new Task("A", _genId());
  _addToMap(oneTask);
  _addToList(oneTask);
  oneTask = new Task("B", _genId());
  _addToMap(oneTask);
  _addToList(oneTask);

  for (final oneTask in taskList) {
    print(oneTask.name);
  }

//   int foundIndex = taskList.indexWhere((taskItem) {
//     taskItem.name.startsWith('B');
//   });

  print("Index found at .. ");

  print(_findInList("A").id);

//   details.forEach((k, v) {
//     print(k);
//     print(v.name);
//   });
}

// Singleton start

class Dao {
  // singleton boilerplate
  Dao._internal() {}
  static final Dao _singleton = new Dao._internal();
  static get I => _singleton;

  int counter = 0;
  // business logic
  void greet() => print("Hello from singleton");
  void incr() {
    counter++;
  }
}

// Singleton end

class Task {
  String name;
  int id;
  Task(this.name, this.id);
}

// DateTime stuff
_showDateFormat() {
  var currDt = DateTime.now();
//   var newDt = DateFormat.yMMMEd().format(currDt);
//   return newDt;
}

// List stuff -------------------------
var taskList = new List<Task>();

_addToList(Task task) {
  taskList.add(task);
}

_removeFromList(int id) {
  taskList.removeWhere((item) => item.id == id);
}

Task _findInList(String query) {
  return taskList.singleWhere((taskItem) => taskItem.name == query);
}

_loopList() {
  for (final oneTask in taskList) {
    print(oneTask.name);
  }
}

// Map stuff -------------------------
var details = new Map<int, Task>();
int _genId() {
  return new DateTime.now().millisecondsSinceEpoch;
}

_addToMap(Task task) {
  details[task.id] = task;
}
Was this helpful?