11part of flutter_parse_sdk;
22
33abstract class ParseBase {
4+
5+ String className;
6+
7+ setClassName (String className) => this .className = className;
8+ String getClassName () => className;
9+
410 /// Stores all the values of a class
511 Map _objectData = Map <String , dynamic >();
612
713 /// Returns [String] objectId
8- String get objectId => _objectData[ 'objectId' ] ;
9- set objectId (String objectId) => _objectData[ objectId] ;
14+ String get objectId => get < String >( OBJECT_ID ) ;
15+ set objectId (String objectId) => set < String >( OBJECT_ID , objectId) ;
1016
1117 /// Returns [DateTime] createdAt
12- DateTime get createdAt => stringToDateTime (_objectData['createdAt' ]);
13- set createdAt (DateTime createdAt) =>
14- _objectData['createdAt' ] = dateTimeToString (createdAt);
18+ DateTime get createdAt => stringToDateTime (get <String >(CREATED_AT ));
19+ set createdAt (DateTime createdAt) => set <String >(CREATED_AT , dateTimeToString (createdAt));
1520
1621 /// Returns [DateTime] updatedAt
17- DateTime get updatedAt => stringToDateTime (_objectData['updatedAt' ]);
18- set updatedAt (DateTime updatedAt) =>
19- _objectData['updatedAt' ] = dateTimeToString (updatedAt);
22+ DateTime get updatedAt => stringToDateTime (get <String >(UPDATED_AT ));
23+ set updatedAt (DateTime updatedAt) => set <String >(UPDATED_AT , dateTimeToString (updatedAt));
2024
2125 /// Converts object to [String] in JSON format
22- @protected
23- toJson () => JsonEncoder ().convert (getObjectData ());
26+ @protected String toJson () {
27+ return JsonEncoder ().convert (getObjectData ());
28+ }
2429
2530 /// Creates a copy of this class
26- @protected
27- copy () => JsonDecoder ().convert (fromJson (getObjectData ()));
31+ @protected copy () => fromJson (JsonDecoder ().convert (toJson ()));
2832
2933 /// Sets all the objects variables
30- @protected
31- setObjectData (Map objectData) => _objectData = objectData;
34+ @protected setObjectData (Map objectData) {
35+ _objectData = objectData;
36+ }
3237
3338 /// Returns the objects variables
34- @protected
35- getObjectData () => _objectData;
39+ @protected getObjectData () {
40+ return _objectData;
41+ }
3642
3743 /// Saves in storage
38- @protected
39- saveInStorage (String key) async {
44+ @protected saveInStorage (String key) async {
4045 await ParseCoreData ().getStore ().setString (key, toJson ());
4146 }
4247
43- @protected
44- fromJson ( Map objectData) {
45- if (_objectData == null ) _objectData = Map ( );
46- _objectData. addAll (objectData) ;
48+ @protected fromJson ( Map objectData) {
49+ if ( getObjectData () == null ) setObjectData ( Map ());
50+ getObjectData (). addAll (objectData );
51+ return this ;
4752 }
4853
49- /// Create a new variable for this object, [bool] forceUpdate is always true,
50- /// if unsure as to whether an item is needed or not, set to false
51- set (String key, dynamic value, {bool forceUpdate: true }) {
54+ /// Sets type [T] from objectData
55+ ///
56+ /// To set an int, call setType<int> and an int will be saved
57+ /// [bool] forceUpdate is always true, if unsure as to whether an item is
58+ /// needed or not, set to false
59+ set <T >(String key, T value, {bool forceUpdate: true }) {
5260 if (value != null ) {
5361 if (getObjectData ().containsKey (key)) {
5462 if (forceUpdate) getObjectData ()[key] = value;
@@ -58,10 +66,18 @@ abstract class ParseBase {
5866 }
5967 }
6068
61- /// Returns a variable from the objectData
62- get (String key, {dynamic defaultValue, bool fromServer}) {
69+ /// Gets type [T] from objectData
70+ ///
71+ /// Returns null or [defaultValue] if provided. To get an int, call
72+ /// getType<int> and an int will be returned, null, or a defaultValue if
73+ /// provided
74+ get <T >(String key, {T defaultValue}) {
6375 if (getObjectData ().containsKey (key)) {
64- return getObjectData ()[key];
76+ if (T != null && getObjectData ()[key] is T ) {
77+ return getObjectData ()[key] as T ;
78+ } else {
79+ return getObjectData ()[key];
80+ }
6581 } else {
6682 return defaultValue;
6783 }
@@ -72,7 +88,8 @@ abstract class ParseBase {
7288 /// Replicates Android SDK pin process and saves object to storage
7389 pin () async {
7490 if (objectId != null ) {
75- await ParseCoreData ().getStore ().setString (objectId, toJson ());
91+ var itemToSave = toJson ();
92+ await ParseCoreData ().getStore ().setString (objectId, itemToSave);
7693 return true ;
7794 } else {
7895 return false ;
@@ -84,8 +101,9 @@ abstract class ParseBase {
84101 /// Replicates Android SDK pin process and saves object to storage
85102 unpin () async {
86103 if (objectId != null ) {
87- var itemToSave = await ParseCoreData ().getStore ().setString (objectId, null );
88- return true ;
104+ var itemToSave = await ParseCoreData ().getStore ().setString (
105+ objectId, null );
106+ if (itemToSave) return true ;
89107 } else {
90108 return false ;
91109 }
@@ -94,9 +112,9 @@ abstract class ParseBase {
94112 /// Saves item to simple key pair value storage
95113 ///
96114 /// Replicates Android SDK pin process and saves object to storage
97- fromPin () async {
115+ fromPin (String objectId) {
98116 if (objectId != null ) {
99- var itemFromStore = await ParseCoreData ().getStore ().getString (objectId);
117+ var itemFromStore = ParseCoreData ().getStore ().getString (objectId);
100118
101119 if (itemFromStore != null ) {
102120 Map <String , dynamic > itemFromStoreMap = JsonDecoder ().convert (
0 commit comments