Separate class and try catch

This commit is contained in:
Valentin CZERYBA 2022-09-13 00:11:43 +02:00
parent 29338f807b
commit e7b4744016
2 changed files with 87 additions and 78 deletions

View File

@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import '../main.dart';
mixin ShowErrorDialog on State<LoginDemo> {
void showErrorDialog(BuildContext context, String text) {
// Create AlertDialog
AlertDialog dialog = AlertDialog(
title: Text("Error"),
content: Text(text),
actions: [
ElevatedButton(
child: Text("OK"),
style: ElevatedButton.styleFrom(
primary: Colors.red,
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
textStyle:
TextStyle(fontSize: 15, fontWeight: FontWeight.normal)),
onPressed: () {
Navigator.of(context).pop("Yes, Of course!"); // Return value
}),
],
);
// Call showDialog function to show dialog.
Future futureValue = showDialog(
context: context,
builder: (BuildContext context) {
return dialog;
});
}
}

View File

@ -4,6 +4,7 @@ import 'MyHomePage.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'classes/alert.dart';
void main() { void main() {
runApp(MyApp()); runApp(MyApp());
@ -24,7 +25,7 @@ class LoginDemo extends StatefulWidget {
_LoginDemoState createState() => _LoginDemoState(); _LoginDemoState createState() => _LoginDemoState();
} }
class _LoginDemoState extends State<LoginDemo> { class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
TextEditingController inputPseudo = TextEditingController(); TextEditingController inputPseudo = TextEditingController();
TextEditingController inputPassword = TextEditingController(); TextEditingController inputPassword = TextEditingController();
Future<void> _login(BuildContext context) async { Future<void> _login(BuildContext context) async {
@ -32,58 +33,62 @@ class _LoginDemoState extends State<LoginDemo> {
var pseudo = inputPseudo.text; var pseudo = inputPseudo.text;
var password = inputPassword.text; var password = inputPassword.text;
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { if ((pseudo.isNotEmpty) && (password.isNotEmpty)) {
String credentials = "${pseudo}:${password}"; try {
Codec<String, String> stringToBase64 = utf8.fuse(base64); String credentials = "${pseudo}:${password}";
String encoded = stringToBase64.encode(credentials); Codec<String, String> stringToBase64 = utf8.fuse(base64);
var response = await http.get(url, headers: { String encoded = stringToBase64.encode(credentials);
HttpHeaders.authorizationHeader: 'Basic $encoded', var response = await http.get(url, headers: {
}); HttpHeaders.authorizationHeader: 'Basic $encoded',
});
if ((response.statusCode == 200) || (response.statusCode == 201)) { if ((response.statusCode == 200) || (response.statusCode == 201)) {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo'))); builder: (_) => MyHomePage(title: 'Flutter Demo')));
} else { } else {
var text = ""; var text = "";
switch (response.statusCode) { switch (response.statusCode) {
case 400: case 400:
{ {
text = "Requête mal construite"; text = "Requête mal construite";
} }
break; break;
case 406: case 406:
{ {
text = "Mot de passe incorrect"; text = "Mot de passe incorrect";
} }
break; break;
case 404: case 404:
{ {
text = "Utilisateur inconnu"; text = "Utilisateur inconnu";
} }
break; break;
case 403: case 403:
{ {
text = "Utilisateur desactive"; text = "Utilisateur desactive";
} }
break; break;
case 410: case 410:
{ {
text = "Token invalide"; text = "Token invalide";
} }
break; break;
case 500: case 500:
{ {
text = "Probleme interne du serveur"; text = "Probleme interne du serveur";
} }
break; break;
default: default:
{ {
text = "Probleme d'authentification inconnu"; text = "Probleme d'authentification inconnu";
} }
break; break;
}
showErrorDialog(context, text);
} }
showErrorDialog(context, text); } catch (e) {
showErrorDialog(context, "${e}");
} }
} else { } else {
showErrorDialog(context, "Champ vide"); showErrorDialog(context, "Champ vide");
@ -169,31 +174,4 @@ class _LoginDemoState extends State<LoginDemo> {
), ),
); );
} }
showErrorDialog(BuildContext context, String text) {
// Create AlertDialog
AlertDialog dialog = AlertDialog(
title: Text("Error"),
content: Text(text),
actions: [
ElevatedButton(
child: Text("OK"),
style: ElevatedButton.styleFrom(
primary: Colors.red,
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
textStyle:
TextStyle(fontSize: 15, fontWeight: FontWeight.normal)),
onPressed: () {
Navigator.of(context).pop("Yes, Of course!"); // Return value
}),
],
);
// Call showDialog function to show dialog.
Future futureValue = showDialog(
context: context,
builder: (BuildContext context) {
return dialog;
});
}
} }