add error for request get

This commit is contained in:
Valentin CZERYBA 2022-09-20 23:04:01 +02:00
parent 53342d5520
commit 0d1ea5d66f
2 changed files with 60 additions and 14 deletions

View File

@ -3,6 +3,7 @@
import 'dart:io'; import 'dart:io';
import 'dart:convert'; import 'dart:convert';
import 'package:covas_mobile/classes/alert.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -54,28 +55,72 @@ class MyHomePage extends StatefulWidget {
State<MyHomePage> createState() => _MyHomePageState(); State<MyHomePage> createState() => _MyHomePageState();
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> with ShowErrorDialog {
String listUser = ""; String listUser = "";
Future<void> _incrementCounter() async { Future<void> _incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
var user = prefs.getString("user"); var user = prefs.getString("user") ?? "";
var jwt = prefs.getString("jwt"); var jwt = prefs.getString("jwt") ?? "";
String former = ""; String former = "";
stderr.writeln("User ${user}");
stderr.writeln("Jwt ${jwt}");
if ((user.isNotEmpty) && (jwt.isNotEmpty)) {
var urlGet = Uri.parse("http://localhost:8083/api/users"); var urlGet = Uri.parse("http://localhost:8083/api/users");
var responseGet = await http var responseGet = await http
.get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'}); .get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'});
stderr.writeln('Response Get status: ${responseGet.statusCode}'); stderr.writeln('Response Get status: ${responseGet.statusCode}');
if (responseGet.statusCode == 200) {
stderr.writeln('Response Get body: ${responseGet.body}'); stderr.writeln('Response Get body: ${responseGet.body}');
var json = jsonDecode(responseGet.body); var json = jsonDecode(responseGet.body);
for (var user in json) { for (var user in json) {
former = "$former\n ${user['name']}"; former = "$former\n ${user['name']}";
} }
} else {
var text = "";
switch (responseGet.statusCode) {
case 400:
{
text = "Requête mal construite";
}
break;
case 406:
{
text = "Mot de passe incorrect";
}
break;
case 404:
{
text = "Utilisateur inconnu";
}
break;
case 403:
{
text = "Utilisateur desactive";
}
break;
case 410:
{
text = "Token invalide";
}
break;
case 500:
{
text = "Probleme interne du serveur";
}
break;
default:
{
text = "Probleme d'authentification inconnu";
}
break;
}
showErrorDialog(context, text);
}
} else {
showErrorDialog(context, "Cache invalide");
}
setState(() { setState(() {
// This call to setState tells the Flutter framework that something has // This call to setState tells the Flutter framework that something has

View File

@ -1,7 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../main.dart'; import '../main.dart';
mixin ShowErrorDialog on State<LoginDemo> { mixin ShowErrorDialog<T extends StatefulWidget> on State<T> {
void showErrorDialog(BuildContext context, String text) { void showErrorDialog(BuildContext context, String text) {
// Create AlertDialog // Create AlertDialog
AlertDialog dialog = AlertDialog( AlertDialog dialog = AlertDialog(