【问题标题】:How To Set Widget ListTile Hidden如何设置 Widget ListTile 隐藏
【发布时间】:2019-12-20 14:42:12
【问题描述】:

我试图设置隐藏小部件ListTile 如果SharedPreferences 获取空数据,我尝试使用Visibility 并且我收到这样的错误

类型“未来”不是类型“小部件”的子类型

我的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 可以隐藏

【问题讨论】:

    标签: android flutter dart hide


    【解决方案1】:

    请尝试以下代码:-

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

    【讨论】:

    • 谢谢老兄,我已经尝试了你的代码和少量定制,这是工作!我创建了一个新函数,我打电话给initState() 非常感谢
    【解决方案2】:

    你的 listTileAju 是异步的,所以它变成了 Future 并且小部件不可能是未来的。

    最简单的解决方案是您必须在 initState 中加载数据,然后您必须在此处使用该数据以避免异步。

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      相关资源
      最近更新 更多