Bonjour à tous,
Lorsque j’essaie de récupérer mon utilisateur Directus pour me Login sur l’application j’obtiens certaines erreurs que je n’arrive pas à comprendre :
- contributors = List.from(
json[« contributors »].map((x) => PermutationsUser.fromJson(x))),
==>
" Une exception s’est produite.
NoSuchMethodError (NoSuchMethodError: The method ‹ map › was called on null. Receiver: null Tried calling: map(Closure: (dynamic) => PermutationsUser))"
ou
la ou je bug c’est que les erreurs ne se produisent pas pour tous mes objets que j’essaie de récupérer alors que je procède de la même manière, et que certains bugs ne se produisent pas lorsque je suis en debug (pas à pas donc peut être un problème d’asynchrone).
Ma function de log :
Future<Token?> loginUser(String email, String password) async {
loading = true;
Token? token;
final response = await http.post(
Uri.parse('https://serveur.test.fr/auth/login'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'email': email, 'password': password}),
);
if (response.statusCode >= 200 && response.statusCode < 300) {
final data = TokenJson.fromJson(jsonDecode(response.body)).data;
token = data;
expirationToken = DateTime.now().add(const Duration(seconds: 850));
currentUser = await getCurrentUser(token: token);
userIsConnected = true;
await verifyIsPayantUser();
print("current User chargé : $currentUser");
loading = false;
} else {
loading = false;
hasErrorLogin = true;
// ignore: avoid_print
print("Error loading");
}
return token;
}
Future<CurrentUser?> getCurrentUser({required Token token}) async {
final accessToken = token.accessToken;
CurrentUser? currentuser;
final response = await http.get(
Uri.parse(
"https://serveur.test.fr/users/me?fields=*,station.*,alerts.id,alerts.alertStation,alerts.alertDepartment,alerts.alertCity,permutations.id,permutations.user_created.id,permutations.user_created.first_name,permutations.user_created.last_name,permutations.user_created.avatar,permutations.user_created.station.id,permutations.user_created.station.service,permutations.user_created.station.department,permutations.user_created.grade,permutations.user_created.deviceToken,permutations.user_created.iosOrAndroid,permutations.user_created.location"),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
HttpHeaders.authorizationHeader: 'Bearer $accessToken'
},
// body: jsonEncode(<String, String>{'email': email, 'password': password}),
);
if (response.statusCode >= 200 && response.statusCode < 300) {
final data = CurrentUserJson.fromJson(jsonDecode(response.body)).data;
currentuser = data;
String? devicToken;
// final deviceToken = UserSettings.getDeviceToken().then((value) {
// devicToken = value;
// });
if (devicToken == "" || devicToken != currentuser.deviceToken) {
await updateCurrentUser(token: token, deviceToken: devicToken);
}
if (currentuser.iosOrAndroid == null ||
currentuser.iosOrAndroid != "Android") {
await updateCurrentUser(
token: token, iosOrAndroid: 'Android', deviceToken: devicToken);
currentUser?.iosOrAndroid = 'Android';
}
print('Utilisateur chargé');
return currentuser;
} else {
print('Impossible de charger lutilisateur');
return null;
}
}
ps : qui n’a rien à voir mais j’ai également une alert dans mon android/app/build.gradle
« throw new GradleException(« Flutter SDK not found. Define location with flutter.sdk in the local.properties file. ») »
alors qu’en vérifiant mon chemin est correct :
sdk.dir=/Users/rm/Library/Android/sdk
flutter.sdk=/Users/rm/flutter
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1