Flutter Firestore 1: improved example

Derive your model classes from BaseModel
abstract class BaseModel {String _id;String get id => _id;

set id(String id) {
this._id = id;
}
BaseModel();Map<String, dynamic> toMap();fromMap(Map<String, dynamic> map); BaseModel createNew();
}
  • toMap()
  • fromMap(Map<String, dynamic> map)
  • createNew()
import 'package:firebase1/model/basemodel.dart';class Note extends BaseModel {
String _title;
String _description;
Note();Note.fromValues(this._title, this._description);Note.fromValuesWithId(String id, this._title, this._description) {
super.id = id;
}
String get title => _title;
String get description => _description;
Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (id != null) {
map['id'] = id;
}
map['title'] = _title;
map['description'] = _description;
return map;
}
Note.fromMap(Map<String, dynamic> map) {
this.id = map['id'];
this._title = map['title'];
this._description = map['description'];
}
Note fromMap(Map<String, dynamic> map) {
return Note.fromMap(map);
}
Note createNew() {
return Note();
}
}

--

--

Partner at Appdnd: agency specialized in App Design & Development. Interests: technology, startups, travel & watersports.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store