I use the getx package in Flutter. I want to authorize the operations that the user can access or perform in the mobile application while logging in to the login page. For this reason, I am sending a list called Permission with Json Web Token. I save this list in the list named _maincontroller.perms that I created during the authentication process. Then, before routing in the same class, I pull the list named _maincontroller.perms and when I print, the data appears in the debug console. When it comes to the main screen, I have a sidebar. In this sidebar, the fields that will appear will be seen according to the authority. For this reason, I need to use the authorizations saved in _maincontroller.perms in the sidebar, but when I print _maincontroller.perms in my sidebar widget, it appears empty.
This is my MainController;
// ignore_for_file: non_constant_identifier_names
import 'package:get/get.dart';
class MainController extends GetxController {
late List<dynamic> perms = <dynamic>[].obs;
final String apiUrl = "http://10.0.2.2:8000/api";
var token = "".obs;
var userid = "".obs;
var name = "".obs;
var surname = "".obs;
var isLogged = false.obs;
var isSuccess = false.obs;
}
this is my auth_controller
// ignore_for_file: avoid_print, use_build_context_synchronously
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:material_tracking/Controllers/main_controller.dart';
import 'package:material_tracking/Services/GetToken.dart';
import 'package:jwt_decoder/jwt_decoder.dart';
class AuthController {
MainController _mainController = MainController();
Future loginUser(String email, String password, BuildContext context) async {
try {
final _url = _mainController.apiUrl + '/Auth/Login';
var response = await http.post(
Uri.parse(_url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({"email": email, "password": password}),
);
if (response.statusCode == 200) {
GetToken result = getTokenFromJson(response.body);
Map<String, dynamic> decodedJwtToken =
JwtDecoder.decode(result.data.accessToken);
if (result.isSuccessful == true) {
_mainController.token = result.data.accessToken.obs;
_mainController.isSuccess = result.isSuccessful.obs;
_mainController.perms = decodedJwtToken["Permission"];
print(_mainController.perms);
if (_mainController.isSuccess == true) {
Navigator.pushNamed(context, "/dashboard");
} else {
print("Hata");
}
} else {
print(result.error);
}
} else {
print("Login Error");
}
} catch (e) {
print("Exception: $e");
}
}
}
This is sidebarnavigation widget
var permissionList = [];
final MainController mainController = Get.find();
late List<dynamic> perms;
class _SideBarNavigationDrawerState extends State<SideBarNavigationDrawer> {
@override
void initState() {
super.initState();
perms = mainController.perms;
if (kDebugMode) {
print(perms);
}
}
This is model.
// To parse this JSON data, do
//
// final getDecodedJwt = getDecodedJwtFromJson(jsonString);
import 'dart:convert';
GetDecodedJwt getDecodedJwtFromJson(String str) =>
GetDecodedJwt.fromJson(json.decode(str));
String getDecodedJwtToJson(GetDecodedJwt data) => json.encode(data.toJson());
class GetDecodedJwt {
String httpSchemasXmlsoapOrgWs200505IdentityClaimsNameidentifier;
String userId;
String email;
String httpSchemasXmlsoapOrgWs200505IdentityClaimsName;
String name;
String surname;
String domainId;
String fullName;
String googleMapApi;
String jti;
List<String> permission;
String aud;
int nbf;
int exp;
String iss;
GetDecodedJwt({
required this.httpSchemasXmlsoapOrgWs200505IdentityClaimsNameidentifier,
required this.userId,
required this.email,
required this.httpSchemasXmlsoapOrgWs200505IdentityClaimsName,
required this.name,
required this.surname,
required this.domainId,
required this.fullName,
required this.googleMapApi,
required this.jti,
required this.permission,
required this.aud,
required this.nbf,
required this.exp,
required this.iss,
});
factory GetDecodedJwt.fromJson(Map<String, dynamic> json) => GetDecodedJwt(
httpSchemasXmlsoapOrgWs200505IdentityClaimsNameidentifier: json[
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"],
userId: json["UserId"],
email: json["email"],
httpSchemasXmlsoapOrgWs200505IdentityClaimsName:
json["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"],
name: json["Name"],
surname: json["Surname"],
domainId: json["DomainId"],
fullName: json["FullName"],
googleMapApi: json["GoogleMapApi"],
jti: json["jti"],
permission: List<String>.from(json["Permission"].map((x) => x)),
aud: json["aud"],
nbf: json["nbf"],
exp: json["exp"],
iss: json["iss"],
);
Map<String, dynamic> toJson() => {
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier":
httpSchemasXmlsoapOrgWs200505IdentityClaimsNameidentifier,
"UserId": userId,
"email": email,
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name":
httpSchemasXmlsoapOrgWs200505IdentityClaimsName,
"Name": name,
"Surname": surname,
"DomainId": domainId,
"FullName": fullName,
"GoogleMapApi": googleMapApi,
"jti": jti,
"Permission": List<dynamic>.from(permission.map((x) => x)),
"aud": aud,
"nbf": nbf,
"exp": exp,
"iss": iss,
};
}
i tryed like this in AuthController print(_mainController.perms); working well in SideBarWidget print(perms); empty result
It's hard to see without seeing the full project but it looks like the problem might be that you are doing
MainController _mainController = MainController();
inside the AuthController
. This means that it has it's very own maincontroller that is in no way linked to the mainController in other parts of the app. You might want to do
MainController _mainController = Get.find();
instead, but again it's hard to judge without seeing the full picture