Flutter 断言失败:第5070行位置12:'&书信电报;优化输出>';:事实并非如此
在用户成功登录并保存状态后,我想隐藏登录屏幕并加载主屏幕,但最终出现了错误 以下断言被抛出到建筑中 导航器-[GlobalObject]键 _WidgetSapState#6686e](脏,依赖项:[UnmanagedStorationScope,HeroControllerScope],状态: 导航状态#c7e9f(股票代码:跟踪1个股票代码)): “package:flatter/src/widgets/navigator.dart”:失败的断言:行 5070位置12:“”不正确 当令牌仍然有效并且只加载主屏幕时,隐藏登录屏幕的正确方法是什么 我的代码 主飞镖Flutter 断言失败:第5070行位置12:'&书信电报;优化输出>';:事实并非如此,flutter,dart,Flutter,Dart,在用户成功登录并保存状态后,我想隐藏登录屏幕并加载主屏幕,但最终出现了错误 以下断言被抛出到建筑中 导航器-[GlobalObject]键 _WidgetSapState#6686e](脏,依赖项:[UnmanagedStorationScope,HeroControllerScope],状态: 导航状态#c7e9f(股票代码:跟踪1个股票代码)): “package:flatter/src/widgets/navigator.dart”:失败的断言:行 5070位置12:“”不正确 当令牌仍然
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'What',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
scaffoldBackgroundColor: Palette.scaffold,
),
// home: SignIn(),
routes: {
//Homepage and being controled by PagesProvider
'/': (context) => SignIn(),
'nav': (context) => NavScreen(),
// add all routes with names here
},
);
}
}
class\u MyAppState扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:什么,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
脚手架背景颜色:Palette.scaffold,
),
//首页:签名(),
路线:{
//主页和由PagesProvider控制
“/”:(上下文)=>SignIn(),
“导航”:(上下文)=>NavScreen(),
//在此处添加所有具有名称的路由
},
);
}
}
我的签名达特
class SignIn extends StatefulWidget {
const SignIn({Key key}) : super(key: key);
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
ProgressDialog progressDialog;
MsalMobile msal;
bool isSignedIn = false;
bool isLoading = true;
@override
void initState() {
super.initState();
MsalMobile.create('assets/auth_config.json', authority).then((client) {
setState(() {
msal = client;
});
refreshSignedInStatus();
});
}
/// Updates the signed in state
refreshSignedInStatus() async {
bool loggedIn = await msal.getSignedIn();
if (loggedIn) {
isSignedIn = loggedIn;
if (isSignedIn) {
dynamic data = await handleGetAccount();
dynamic token = await handleGetTokenSilently();
dynamic result = token;
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
sharedPreferences.get("username");
sharedPreferences.get("token");
print('access token (truncated): ${result.accessToken}');
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => NavScreen(),
),
);
}
// Remaining code for navigation
}
}
/// Gets a token silently.
Future<dynamic> handleGetTokenSilently() async {
String authority = "https://login.microsoftonline.com/$TENANT_ID";
final result = await msal.acquireTokenSilent([SCOPE], authority);
if (result != null) {
// print('access token (truncated): ${result.accessToken}');
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
sharedPreferences.setString("token", result.accessToken);
return result;
} else {
print('no access token');
return null;
}
}
/// Signs a user in
handleSignIn() async {
await msal.signIn(null, [SCOPE]).then((result) {
// ignore: unnecessary_statements
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.
Future<dynamic> handleGetAccount() async {
// <-- Replace dynamic with type of currentAccount
final result = await msal.getAccount();
if (result.currentAccount != null) {
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
sharedPreferences.setString("username", result.currentAccount.username);
//print(result.currentAccount.username);
return result.currentAccount;
} else {
print('no account found');
return null;
}
}
@override
Widget build(BuildContext context) {
progressDialog = ProgressDialog(context, type:ProgressDialogType.Normal, isDismissible: false, );
return MaterialApp(
home: new Scaffold(
body: Builder(
builder: (context) => Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Image.asset('assets/landing.webp',
fit: BoxFit.fill,
color: Color.fromRGBO(255, 255, 255, 0.6),
colorBlendMode: BlendMode.modulate),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 10.0),
Container(
width: 130.0,
child: Align(
alignment: Alignment.center,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: Color(0xffffffff),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(
FontAwesomeIcons.microsoft,
color: Color(0xFF01A6F0),
),
// Visibility(
// visible: !isSignedIn,
SizedBox(width: 10.0),
Text(
'Sign in',
style: TextStyle(
color: Colors.black, fontSize: 18.0),
),
// child: RaisedButton(
// child: Text("Sign In"),
// onPressed: handleSignIn,
// ),
// ),
],
),
onPressed: () => {
progressDialog.show(),
handleSignIn(),
progressDialog.hide()
})),
)
],
),
],
),
),
));
}
}
类登录扩展StatefulWidget{
const SignIn({Key}):super(Key:Key);
@凌驾
_SignInState createState()=>\u SignInState();
}
类_SignInState扩展状态{
进行对话进行对话;
MsalMobile msal;
bool-isSignedIn=false;
bool isLoading=true;
@凌驾
void initState(){
super.initState();
create('assets/auth_config.json',authority)。然后((客户端){
设置状态(){
msal=客户;
});
RefreshSignedStatus();
});
}
///更新已登录状态
RefreshSignedStatus()异步{
bool loggedIn=wait msal.getSignedIn();
if(loggedIn){
isSignedIn=loggedIn;
如果(伊西涅丁){
动态数据=等待handleGetAccount();
动态令牌=等待HandleGetTokensilly();
动态结果=令牌;
SharedReferences SharedReferences=
等待SharedReferences.getInstance();
SharedReferences.get(“用户名”);
SharedReferences.get(“令牌”);
打印('访问令牌(截断):${result.accessToken}');
Navigator.of(context.pop();
导航器.of(上下文).pushReplacement(
材料路线(
生成器:(上下文)=>NavScreen(),
),
);
}
//导航的剩余代码
}
}
///以静默方式获取令牌。
Future HandleGetTokensilenly()异步{
字符串权限=”https://login.microsoftonline.com/$TENANT_ID“;
最终结果=等待msal.acquireTokenSilent([范围],权限);
如果(结果!=null){
//打印('访问令牌(截断):${result.accessToken}');
SharedReferences SharedReferences=
等待SharedReferences.getInstance();
SharedReferences.setString(“token”,result.accessToken);
返回结果;
}否则{
打印(“无访问令牌”);
返回null;
}
}
///在中为用户签名
handleSignIn()异步{
等待msal.SIGN(空,[范围])。然后((结果){
//忽略:不必要的_语句
RefreshSignedStatus();
}).catchError((异常){
if(例外情况为MsalMobileException){
logMsalMobileError(例外);
}否则{
最终ex=作为例外的例外;
打印(“发生异常”);
打印(例如toString());
}
});
}
logMsalMobileError(MsalMobileException异常){
打印(“${exception.errorCode}:${exception.message}”);
if(exception.innerException!=null){
印刷品(
'内部异常=${exception.innerException.errorCode}:${exception.innerException.message}');
}
}
///将用户注销。
handleSignOut()异步{
试一试{
打印(“注销”);
等待msal.signOut();
打印(“签出完成”);
RefreshSignedStatus();
}关于MsalMobileException捕获(异常){
logMsalMobileError(例外);
}
}
///获取当前帐户和以前的帐户。
Future handleGetAccount()异步{
//堆叠(
fit:StackFit.expand,
儿童:[
容器(
宽度:MediaQuery.of(context).size.width,
高度:MediaQuery.of(context).size.height,
子项:Image.asset('assets/landing.webp',
fit:BoxFit.fill,
颜色:color.fromRGBO(255,255,255,0.6),
colorBlendMode:BlendMode.modulate),
),
纵队(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
尺寸箱(高度:10.0),
容器(
宽度:130.0,
子对象:对齐(
对齐:对齐.center,
孩子:升起按钮(
形状:圆形矩形边框(
边界半径:新边界半径。圆形(30.0)),
颜色:颜色(0xFFFFFF),
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
图标(
FontAwesomeIcons.microsoft,
颜色:颜色(0xFF01A6F0),
),
//可见度(
//可见:!isSignedIn,
尺寸箱(宽度:10.0),
正文(
“登录”,
样式:TextStyle(
颜色:颜色。黑色,字体大小:18.0),
),
routes: {
Homepage and being controled by PagesProvider
'nav': (context) => NavScreen(),
'home': (context) => HomePage(),
// add all routes with names here
},
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => NavScreen(),
),
);
Navigator.pushNamed(context, 'nav');