如果ListTile
获取空数据,我试图设置隐藏的窗口小部件SharedPreferences
,我不得不尝试使用Visibility
并且出现这样的错误
“ Future”类型不是“ Widget”类型的子类型
我的Widget build(BuildContext context)
Widget build(BuildContext context) {
return new Drawer(
child: new ListView(
children: <Widget>[
new DrawerHeader(
child: new Text("Menu"),
decoration: new BoxDecoration(color: Colors.lightBlueAccent),
),
new ListTile(
title: new Text("Profile"),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ProfileScreen()));
},
),
new ListTile(
title: new Text("Proposal List"),
onTap: () {
visibleMethod();
},
),
//MY PROBLEM HERE
Visibility(
visible: true,
child: listTileAju()),
new ListTile(
title: new Text("Sign Out"),
onTap: () async {
SharedPreferences pref = await SharedPreferences.getInstance();
pref.remove("authorization");
pref.remove("is_login");
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext ctx) => LoginPage()));
},
),
],
));
}
还有My Widget listTileAju()
listTileAju() async {
SharedPreferences pref = await SharedPreferences.getInstance();
setState(() {
roleAju = (pref.getStringList('role_aju') ?? 'Something Went Wrong');
});
if (roleAju != null) {
Visibility(
visible: true,
child: new ListTile(
title: new Text("Aju List"),
onTap: () {
Navigator.pop(context);
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new AjuScreen()));
},
),
);
} else {
Visibility(
visible: false,
child: new ListTile(
title: new Text("Aju List"),
onTap: null,
),
);
}
}
我希望如果SharedPreferences获取空数据,则可以隐藏小部件ListTile
答案 0 :(得分:0)
您的listTileAju是异步的,因此它成为Future,而小部件不能成为Future。
最简单的解决方案是您必须将数据加载到initState中,然后必须在此处使用该数据以避免异步。
答案 1 :(得分:0)
请尝试以下代码:-
bool isShowListTile;
Widget listTileAju(){
Visibility(
visible: isShowListTile,
child: new ListTile(
title: new Text("Aju List"),
onTap: () {
Navigator.pop(context);
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new AjuScreen()));
},
),
);
}
@override
void initState() {
super.initState();
SharedPreferences pref = await SharedPreferences.getInstance();
roleAju = (pref.getStringList('role_aju') ?? 'Something Went Wrong');
if (roleAju != null) {
if (mounted) {
setState(() {
isShowListTile = true;
});
}
} else {
if (mounted) {
setState(() {
isShowListTile = false;
});
}
}
}