1
1
part of flutter_parse_sdk;
2
2
3
3
abstract class ParseBase {
4
+
5
+ String className;
6
+
7
+ setClassName (String className) => this .className = className;
8
+ String getClassName () => className;
9
+
4
10
/// Stores all the values of a class
5
11
Map _objectData = Map <String , dynamic >();
6
12
7
13
/// 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) ;
10
16
11
17
/// 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));
15
20
16
21
/// 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));
20
24
21
25
/// Converts object to [String] in JSON format
22
- @protected
23
- toJson () => JsonEncoder ().convert (getObjectData ());
26
+ @protected String toJson () {
27
+ return JsonEncoder ().convert (getObjectData ());
28
+ }
24
29
25
30
/// Creates a copy of this class
26
- @protected
27
- copy () => JsonDecoder ().convert (fromJson (getObjectData ()));
31
+ @protected copy () => fromJson (JsonDecoder ().convert (toJson ()));
28
32
29
33
/// Sets all the objects variables
30
- @protected
31
- setObjectData (Map objectData) => _objectData = objectData;
34
+ @protected setObjectData (Map objectData) {
35
+ _objectData = objectData;
36
+ }
32
37
33
38
/// Returns the objects variables
34
- @protected
35
- getObjectData () => _objectData;
39
+ @protected getObjectData () {
40
+ return _objectData;
41
+ }
36
42
37
43
/// Saves in storage
38
- @protected
39
- saveInStorage (String key) async {
44
+ @protected saveInStorage (String key) async {
40
45
await ParseCoreData ().getStore ().setString (key, toJson ());
41
46
}
42
47
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 ;
47
52
}
48
53
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 }) {
52
60
if (value != null ) {
53
61
if (getObjectData ().containsKey (key)) {
54
62
if (forceUpdate) getObjectData ()[key] = value;
@@ -58,10 +66,18 @@ abstract class ParseBase {
58
66
}
59
67
}
60
68
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}) {
63
75
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
+ }
65
81
} else {
66
82
return defaultValue;
67
83
}
@@ -72,7 +88,8 @@ abstract class ParseBase {
72
88
/// Replicates Android SDK pin process and saves object to storage
73
89
pin () async {
74
90
if (objectId != null ) {
75
- await ParseCoreData ().getStore ().setString (objectId, toJson ());
91
+ var itemToSave = toJson ();
92
+ await ParseCoreData ().getStore ().setString (objectId, itemToSave);
76
93
return true ;
77
94
} else {
78
95
return false ;
@@ -84,8 +101,9 @@ abstract class ParseBase {
84
101
/// Replicates Android SDK pin process and saves object to storage
85
102
unpin () async {
86
103
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 ;
89
107
} else {
90
108
return false ;
91
109
}
@@ -94,9 +112,9 @@ abstract class ParseBase {
94
112
/// Saves item to simple key pair value storage
95
113
///
96
114
/// Replicates Android SDK pin process and saves object to storage
97
- fromPin () async {
115
+ fromPin (String objectId) {
98
116
if (objectId != null ) {
99
- var itemFromStore = await ParseCoreData ().getStore ().getString (objectId);
117
+ var itemFromStore = ParseCoreData ().getStore ().getString (objectId);
100
118
101
119
if (itemFromStore != null ) {
102
120
Map <String , dynamic > itemFromStoreMap = JsonDecoder ().convert (
0 commit comments