Flutter Azure AD Auth:如何显示在登录到另一个屏幕期间检索到的用户电子邮件和名称 [英] Flutter Azure AD Auth: How to display the user email and name retrieved during login to another screen
问题描述
我是新手,我已经从Azure AD Auth中成功检索了用户电子邮件和用户名.但是我想在主屏幕的文本"小部件中显示它.如何将用户名从Signin类发送到主屏幕并显示名称?
I am new to flutter, I have successfully retrieved the user email and username from Azure AD Auth. But I want to display this in Text widget in home screen. How can I send the username from the Signin class to home screen and display the name?
登录
class _SignInState extends State<SignIn> {
MsalMobile msal;
bool isSignedIn = false;
@override
void initState() {
super.initState();
MsalMobile.create('assets/auth_config.json', authority).then((client) {
setState(() {
msal = client;
});
refreshSignedInStatus();
});
}
/// Updates the signed in state
refreshSignedInStatus() {
msal.getSignedIn().then((loggedIn) {
print('refreshing');
setState(() {
isSignedIn = loggedIn;
if(isSignedIn) {
// Your navigation code
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => NavScreen()));
}
});
});
}
/// Signs a user in
handleSignIn() async {
await msal.signIn(null, [SCOPE]).then((result) {
refreshSignedInStatus();
}).catchError((exception) {
if (exception is MsalMobileException) {
logMsalMobileError(exception);
} else {
final ex = exception as Exception;
print('exception occurred');
print(ex.toString());
}
}
);
}
logMsalMobileError(MsalMobileException exception) {
print('${exception.errorCode}: ${exception.message}');
if (exception.innerException != null) {
print(
'inner exception = ${exception.innerException.errorCode}: ${exception.innerException.message}');
}
}
/// Signs a user out.
handleSignOut() async {
try {
print('signing out');
await msal.signOut();
print('signout done');
refreshSignedInStatus();
} on MsalMobileException catch (exception) {
logMsalMobileError(exception);
}
}
/// Gets the current and prior accounts.
handleGetAccount() async {
await msal.getAccount().then((result) {
if (result.currentAccount != null) {
return result.currentAccount.username;
} else {
print('no account found');
}
}).catchError((exception) {
if (exception is MsalMobileException) {
logMsalMobileError(exception);
} else {
print('exception occurred');
}
});
}
主屏幕
class _HomeScreenDesktop extends StatelessWidget {
final SignIn handleGetAccount;
final TrackingScrollController scrollController;
const _HomeScreenDesktop({Key key, this.scrollController, this.handleGetAccount}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Flexible(
flex: 2,
child: Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: MoreOptionsList(currentUser: currentUser),
),
),
),
const Spacer(),
Container(
height: 1000.0,
width: 600.0,
child: ListView(
controller: scrollController,
children: <Widget>[
SizedBox(
height: 30,
),
Padding(
padding: EdgeInsets.only(left: 16, right: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Show Retrieved username from azure account",
style: GoogleFonts.openSans(
textStyle: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold)),
),
SizedBox(
height: 4,
),
],
),
IconButton(
alignment: Alignment.topRight,
icon: Image.asset(
"assets/notification.png",
width: 24,
),
onPressed: () => {},
),
],
),
),
SizedBox(
height: 20,
),
GridDashboardwe()
],
)),
const Spacer(),
Flexible(
flex: 2,
child: Container(
color: Color(0xff392850) ,
),
)
],
);
}
}
我正在登录屏幕上使用此方法来获取帐户详细信息
I am using this method on the signin screen to get the account details
/// Gets the current and prior accounts.
handleGetAccount() async {
await msal.getAccount().then((result) {
if (result.currentAccount != null) {
return result.currentAccount.username;
} else {
print('no account found');
}
}).catchError((exception) {
if (exception is MsalMobileException) {
logMsalMobileError(exception);
} else {
print('exception occurred');
}
});
}
推荐答案
您需要通过参数将用户对象从 SignIn
类传递到 _HomeScreenDesktop
小部件.另外,您应该将主屏幕小部件设为公开,而不是设为私有.在类名之前使用下划线表示它是私有的,即,使用 HomeScreenDesktop
代替 _HomeScreenDesktop
.
You need to pass the user object via parameters from the SignIn
class to the _HomeScreenDesktop
widget. Also, you should make the home screen widget public instead of making it private. Using an underscore before the class name means it is private ie, use HomeScreenDesktop
instead of _HomeScreenDesktop
.
还请指定方法的返回类型,这将非常有帮助.
Also please specify the return type of methods which will be really very helpful.
更新的 handleGetAccount
方法:
Future<dynamic> handleGetAccount() async { // <-- Replace dynamic with type of currentAccount
await msal.getAccount().then((result) {
if (result.currentAccount != null) {
return result.currentAccount;
} else {
print('no account found');
return null;
}
}).catchError((exception) {
if (exception is MsalMobileException) {
logMsalMobileError(exception);
} else {
print('exception occurred');
return null;
}
});
调用方法 handleGetAccount
来获取当前登录的用户&然后您可以将此用户对象传递给 HomeScreenWidget
,该外观将如下所示:
Call method handleGetAccount
to get current signed-in user & then you can pass this user object to the HomeScreenWidget
, which will look something like this:
/// Updates the signed in state
refreshSignedInStatus() {
msal.getSignedIn().then((loggedIn) {
print('refreshing');
setState(() {
isSignedIn = loggedIn;
if(isSignedIn) {
dynamic currentAccount = await handleGetAccount();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => NavScreen(
currentAccount: currentAccount, // <--- Passing required named parameter "currentAccount" in NavScreen widget.
),
),
);
}
});
});
}
NavScreen
小部件也将通过创建如下的构造函数进行更新:
The NavScreen
widget will also be updated by creating a constructor as follows:
NavScreen({
@required this.currentAccount, // Required currentUser parameter
});
final dynamic currentAccount; // Replace dynamic with the type of currentUser
从 NavScreen
,我想您必须以某种方式导航到 HomeScreenDesktop
,但是在执行此操作时,您需要将此 currentAccount
对象传递给 HomeScreenDesktop
&创建相同的构造函数参数如 NavScreen
小部件中所示(类似这样):
From NavScreen
, I guess you must be navigating to HomeScreenDesktop
somehow, but while doing this, you need to pass this currentAccount
object to the HomeScreenDesktop
& create the same constructor & parameter as in NavScreen
widget (something like this):
HomeScreenDesktop({
@required this.currentAccount, // Required currentUser parameter
});
final dynamic currentAccount; // Replace dynamic with the type of currentUser
您还必须在导航时传递 currentAccount
对象.看起来像这样:
You will also have to pass the currentAccount
object while navigating.
It will look something like this:
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => HomeScreenDesktop(
currentAccount: currentAccount,
),
),
);
这篇关于Flutter Azure AD Auth:如何显示在登录到另一个屏幕期间检索到的用户电子邮件和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!