Skip to content

Instantly share code, notes, and snippets.

@rizkyhaksono
Created July 21, 2022 17:38
Show Gist options
  • Save rizkyhaksono/f23f0d3c4f57cccef063dd543a246d3b to your computer and use it in GitHub Desktop.
Save rizkyhaksono/f23f0d3c4f57cccef063dd543a246d3b to your computer and use it in GitHub Desktop.
Login Method with API
var usernameController = TextEditingController();
var passwordController = TextEditingController();
Future<void> loginMethod() async {
if (usernameController.text.isNotEmpty && passwordController.text.isNotEmpty) {
var response = await http.post(
Uri.parse(
// your API
""),
body: ({
'username': usernameController.text,
'password': passwordController.text,
}),
);
if (response.statusCode == 200) {
// check the response
print("Response Status: ${response.statusCode}");
} else {
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Your data is incorrect!")));
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Username and password must be written!")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment