Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas
Last active July 4, 2020 07:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatilShreyas/3b19df2e464fec7de4c70f20fc567ad3 to your computer and use it in GitHub Desktop.
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);
}
}
}
@albazy
Copy link

albazy commented Jan 20, 2020

Thanks for such a great job . I tried the codes and it worked very well on my computer and on android virtual compiler where when I installed the app on my android phone it woked too but didnt submet the info to the Gsheet , I mean the submetting message shows up but the "Submetted" message didnt and as a result wasnt able to send them to gsheet . Can you help me to figure out the problem ?? Thanks in advanced !

@PatilShreyas
Copy link
Author

I'll suggest you to print Http response so that you'll get know about response coming.

@albazy
Copy link

albazy commented Feb 14, 2020

thanks for your response , but as ı still new at programming I would ask you to give m a hint for that

@PatilShreyas
Copy link
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