Muhammad Faisal
Muhammad Faisal

Reputation: 127

How To Set Widget ListTile Hidden

im trying to set hidden widget ListTile if SharedPreferences Getting Null Data , i have try to using Visibility and i getting error like this

type 'Future' is not a subtype of type 'Widget'

My 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()));
          },
        ),
      ],
    ));
  }

And 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,
        ),
      );
    }
  }

And i hope the widget ListTile can be Hidden if SharedPreferences Getting Null Data

Upvotes: 0

Views: 2298

Answers (2)

Dhaval Patel
Dhaval Patel

Reputation: 409

Please try below code:-

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;
      });
    }
  }
}

Upvotes: 1

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27167

Your listTileAju is async, so it become Future and widget can't be future.

Simplest solution is that you have to load data in initState and then you have to use that data here to avoid async.

Upvotes: 0

Related Questions