Created
January 21, 2019 06:06
-
-
Save chadidi/48f4a3630fc5d947d39b67e19f48a994 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<Map<String, dynamic>> createActivity(activityData) async { | |
print('creating activity...'); | |
var url = "https://www.tapographics.com/api/activity"; | |
print(activityData); | |
final http.Response response = | |
await http.post(Uri.encodeFull(url), body: json.encode(activityData)); | |
// debug | |
print("Response status: ${response.statusCode}"); | |
print("Response status: ${response.body}"); | |
bool hasError = true; | |
String message = 'Something went wrong.'; | |
if (response.statusCode != 201) { | |
message = 'Request failed! try again.'; | |
} else { | |
hasError = false; | |
message = 'Request successed!'; | |
Activity activity = Activity( | |
userId: activityData['userId'], | |
name: activityData['name'], | |
description: activityData['description'], | |
startAt: activityData['startAt'], | |
endsAt: activityData['endsAt'], | |
lat: activityData['lat'], | |
long: activityData['long'], | |
category: activityData['category'], | |
); | |
addActivity(activity); | |
notifyListeners(); | |
} | |
return {'success': !hasError, 'message': message}; | |
} | |
// this function calls the one above | |
void save() async { | |
_extractMapInfo(); | |
final Map<String, dynamic> activityData = { | |
"userId": 1, | |
"name": activityName.text, | |
"description": activityDescription.text, | |
"startAt": activityStartAt.text, | |
"endsAt": activityEndAt.text, | |
"lat": _latitude, | |
"long": _longitude, | |
"category": 2, | |
"status": "pending" | |
}; | |
model.createActivity(activityData).then((result) { | |
print(result['message']); | |
if (result['success']) { | |
print('activity created!'); | |
// Send user to main screen | |
// Navigator.of(context).pushReplacementNamed('/home'); | |
} else { | |
print('activity failed to save!'); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment