Reputation: 127
I'm trying to get data from shared preferences, when data is No Access
Some Menu Will Be Hidden.
But I'm getting an error like this:
I/flutter (15955): Another exception was thrown: 'package:flutter/src/widgets/visibility.dart': Failed assertion: line 65 pos 15: 'visible != null': is not true.
Okay , may this is not made my app force close , but i always getting red screen just flicker if i will be open drawer
NOTE : VIEW / HIDDEN WIDGET WORKING , JUST A PROBLEM IN 'VISIBLE != TRUE'
Here is my code
///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();
},
),
],
));
}
}
Upvotes: 3
Views: 12899
Reputation: 1063
Just for those who may have the same error: Failed assertion: line 65 pos 15: 'visible != null': is not true even after having the _someVisible bool variable set either outside or inside the initState, the error persisted. In my case, navigating from a drawer or from another page to a page containing visibility widget, the function call onPressed: somefunction was the culprit. Initially was:
onPressed:() somefunction(),//same error on visible !=null
Later changed to:
onPressed: somefunction(),//check that the first () have been removed - same error
Changed to:
onPressed: somefunction, //WORKED
SOME FUNCTION:
somefunction() {
setState(() {
_someVisible = !_someVisible;
});
}
Upvotes: 0
Reputation: 116
You need to initialize the menuAju
variable declared at the top of your widget. The build method is being run before your initialization logic, because your _loadMenuAju
method is declared as async
.
Just change this line:
bool menuAju;
To this:
bool menuAju = false;
Upvotes: 6