当数据为No Access
某些菜单将被隐藏时,我正在尝试从共享的首选项中获取数据。
但是我遇到这样的错误:
I / flutter(15955):引发了另一个异常:'package:flutter / src / widgets / visibility.dart':失败的断言:65行pos 15:'visible!= null':不正确。
好的,也许这不是使我的应用强制关闭,但是如果我要打开抽屉,我总是会红屏,只是闪烁
注意:查看/隐藏小工具的工作,只是“可见!=真”中的一个问题
这是我的代码
///Widget for creating drawer menu in the sidebar.
import 'package:flutter/material.dart';
import 'package:flutter_ebudgeting/screens/login_page.dart';
import 'package:flutter_ebudgeting/screens/aju/AjuScreen.dart';
import 'package:flutter_ebudgeting/screens/proposal/ProposalScreen.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:flutter_ebudgeting/screens/profile/ProfileScreen.dart';
List roleAju;
class DrawerOnly extends StatefulWidget {
@override
_DrawerOnlyState createState() => _DrawerOnlyState();
}
class _DrawerOnlyState extends State<DrawerOnly> {
bool menuAju;
String nulled = "[No Access, No Access, No Access]";
@override
void initState() {
super.initState();
_loadMenuAju();
}
void _loadMenuAju() async {
SharedPreferences pref = await SharedPreferences.getInstance();
roleAju = (pref.getStringList('role_aju'));
if (roleAju.toString() == nulled){
setState((){
menuAju = false;
});
}else{
setState((){
menuAju = true;
});
}
}
@override
Widget build(BuildContext context) {
return new Drawer(
child: new ListView(
children: <Widget>[
new DrawerHeader(
child: new Text("Menu"),
decoration: new BoxDecoration(
gradient: LinearGradient(
colors: [Colors.lightBlueAccent, Colors.lightGreenAccent]),
),
),
///Menu to go to Profile
new ListTile(
leading: Icon(Icons.person_outline),
title: new Text("Profile"),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ProfileScreen()));
},
),
///Menu to go to Proposal List
new ListTile(
leading: Icon(Icons.view_list),
title: new Text("Proposal List"),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ProposalScreen()));
},
),
///Menu to go to AJU List
new Visibility(
visible: menuAju,
child: ListTile(
leading: Icon(Icons.view_list),
title: new Text("Aju List"),
onTap: (){
Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context) => new AjuScreen()));
},
)
),
///Menu to log out and return to login page.
new ListTile(
leading: Icon(EvaIcons.logOut),
title: new Text("Sign Out"),
onTap: () {
Alert(
context: context,
type: AlertType.warning,
title: "SIGN OUT CONFIRMATION",
desc: "Are you sure you want to sign out?",
buttons: [
DialogButton(
child: Text(
"NO",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
color: Colors.red),
DialogButton(
child: Text(
"YES",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () async {
SharedPreferences pref =
await SharedPreferences.getInstance();
pref.remove("authorization");
pref.remove("is_login");
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
new LoginPage()));
showSimpleNotification(
Text("Successfully signed out."),
background: Colors.green,
);
},
gradient: LinearGradient(
colors: [Colors.greenAccent, Colors.green]),
)
],
).show();
},
),
],
));
}
}
答案 0 :(得分:0)
您需要初始化在小部件顶部声明的menuAju
变量。由于您的_loadMenuAju
方法被声明为async
,因此在初始化逻辑之前运行了build方法。
只需更改此行:
bool menuAju;
对此:
bool menuAju = false;
答案 1 :(得分:0)
对于那些可能具有相同错误的用户:失败的断言:65行pos 15:'visible!= null':即使在initState外部或内部设置了_someVisible bool变量后,它也不是正确的,该错误仍然存在。 就我而言,从抽屉或另一页导航到包含可见性小部件的页面,函数onPressed:somefunction是罪魁祸首。 最初是:
onPressed:() somefunction(),//same error on visible !=null
后来更改为:
onPressed: somefunction(),//check that the first () have been removed - same error
更改为:
onPressed: somefunction, //WORKED
某些功能:
somefunction() {
setState(() {
_someVisible = !_someVisible;
});
}