Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas
Last active July 4, 2020 07:36
Show Gist options
  • Select an option

  • Save PatilShreyas/3b19df2e464fec7de4c70f20fc567ad3 to your computer and use it in GitHub Desktop.

Select an option

Save PatilShreyas/3b19df2e464fec7de4c70f20fc567ad3 to your computer and use it in GitHub Desktop.
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import '../model/form.dart';
/// FormController is a class which does work of saving FeedbackForm in Google Sheets using
/// HTTP GET request on Google App Script Web URL and parses response and sends result callback.
class FormController {
// Google App Script Web URL.
static const String URL = "https://script.google.com/macros/s/AKfycbyAaNh-1JK5pSrUnJ34Scp3889mTMuFI86DkDp42EkWiSOOycE/exec";
// Success Status Message
static const STATUS_SUCCESS = "SUCCESS";
/// Async function which saves feedback, parses [feedbackForm] parameters
/// and sends HTTP GET request on [URL]. On successful response, [callback] is called.
void submitForm(
FeedbackForm feedbackForm, void Function(String) callback) async {
try {
await http.post(URL, body: feedbackForm.toJson()).then((response) async {
if (response.statusCode == 302) {
var url = response.headers['location'];
await http.get(url).then((response) {
callback(convert.jsonDecode(response.body)['status']);
});
} else {
callback(convert.jsonDecode(response.body)['status']);
}
});
} catch (e) {
print(e);
}
}
}
@PatilShreyas
Copy link
Copy Markdown
Author

PatilShreyas commented Feb 14, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment