-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathmain.dart
182 lines (144 loc) · 5.34 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import 'package:flutter/material.dart';
import 'package:flutter_plugin_example/application_constants.dart';
import 'package:flutter_plugin_example/diet_plan.dart';
import 'package:parse_server_sdk/parse.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
initParse();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('Running Parse init'),
),
floatingActionButton:
new FloatingActionButton(onPressed: runTestQueries),
),
);
}
initParse() async {
// Initialize parse
Parse().initialize(ApplicationConstants.keyParseApplicationId,
ApplicationConstants.keyParseServerUrl,
masterKey: ApplicationConstants.keyParseMasterKey,
appName: ApplicationConstants.keyAppName,
debug: true);
// Check server is healthy and live - Debug is on in this instance so check logs for result
var response = await Parse().healthCheck();
if (response.success){
runTestQueries();
} else {
print("Server health check failed");
}
}
runTestQueries() {
//createItem();
//getAllItems();
//getAllItemsByName();
//getSingleItem();
query();
//function();
//initUser();
}
void createItem() async {
var newObject = ParseObject('TestObjectForApi');
newObject.set<String>('name', 'testItem');
newObject.set<int>('age', 26);
var apiResponse = await newObject.create();
if (apiResponse.success && apiResponse.result != null) {
print(ApplicationConstants.keyAppName + ": " + apiResponse.result.toString());
}
}
void getAllItemsByName() async {
var apiResponse = await ParseObject('ParseTableName').getAll();
if (apiResponse.success && apiResponse.result != null) {
for (var testObject in apiResponse.result) {
print(ApplicationConstants.keyAppName + ": " + testObject.toString());
}
}
}
void getAllItems() async {
var apiResponse = await DietPlan().getAll();
if (apiResponse.success && apiResponse.result != null) {
for (var plan in apiResponse.result) {
print(ApplicationConstants.keyAppName + ": " + (plan as DietPlan).name);
}
} else {
print(ApplicationConstants.keyAppName + ": " + apiResponse.error.message);
}
}
void getSingleItem() async {
var apiResponse = await DietPlan().getObject('R5EonpUDWy');
if (apiResponse.success && apiResponse.result != null) {
var dietPlan = (apiResponse.result as DietPlan);
// Shows example of storing values in their proper type and retrieving them
dietPlan.set<int>('RandomInt', 8);
var randomInt = dietPlan.get<int>('RandomInt');
if (randomInt is int) print('Saving generic value worked!');
// Shows example of pinning an item
dietPlan.pin();
// shows example of retrieving a pin
var newDietPlanFromPin = DietPlan().fromPin('R5EonpUDWy');
if (newDietPlanFromPin != null) print('Retreiving from pin worked!');
} else {
print(ApplicationConstants.keyAppName + ": " + apiResponse.error.message);
}
}
void query() async {
var queryBuilder = QueryBuilder<DietPlan>(DietPlan())
..whereContains(DietPlan.keyName, "iet")
..keysToReturn([DietPlan.keyName]);
var apiResponse = await queryBuilder.query();
if (apiResponse.success && apiResponse.result != null) {
print("Result: ${((apiResponse.result as List<dynamic>).first as DietPlan).toString()}");
} else {
print("Result: ${apiResponse.error.message}");
}
}
initUser() async {
// All return type ParseUser except all
var user = ParseUser("TestFlutter", "TestPassword123", "TestFlutterSDK@gmail.com");
var response = await user.#();
if (response.success) user = response.result;
response = await user.login();
if (response.success) user = response.result;
user = null;
// Best practice for starting the app. This will check for a valid user
user = await ParseUser.currentUser();
await user.logout();
user = await ParseUser.currentUser();
response = await user.getCurrentUserFromServer();
if (response.success) user = response.result;
response = await user.requestPasswordReset();
if (response.success) user = response.result;
response = await user.verificationEmailRequest();
if (response.success) user = response.result;
response = await user.save();
if (response.success) user = response.result;
var destroyResponse = await user.destroy();
if (destroyResponse.success) print('object has been destroyed!');
// Returns type ParseResponse as its a query, not a single result
response = await ParseUser.all();
if (response.success) user = response.result;
var queryBuilder = QueryBuilder<ParseUser>(ParseUser.forQuery())
..whereStartsWith(ParseUser.keyUsername, 'phillw');
var apiResponse = await queryBuilder.query();
if (apiResponse.success) user = response.result;
}
function() {
var function = ParseCloudFunction('testFunction');
function.execute();
}
}