断言失败:第 65 行第 15 行:'visible != null':不正确

Failed assertion: line 65 pos 15: 'visible != null': is not true

我正在尝试从共享首选项中获取数据,当数据为 No Access 某些菜单将被隐藏时。

但是我收到这样的错误:

I/flutter (15955): Another exception was thrown: 'package:flutter/src/widgets/visibility.dart': Failed assertion: line 65 pos 15: 'visible != null': is not true.

好的,这可能不会让我的应用程序强制关闭,但如果我要打开抽屉,我总是会出现红屏只是闪烁

NOTE : VIEW / HIDDEN WIDGET WORKING , JUST A PROBLEM IN 'VISIBLE != TRUE'

这是我的代码

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

您需要初始化在小部件顶部声明的 menuAju 变量。构建方法在您的初始化逻辑之前 运行,因为您的 _loadMenuAju 方法被声明为 async.

只需更改此行:

bool menuAju;

为此:

bool menuAju = false;

对于那些可能有相同错误的人:断言失败:第 65 行第 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;
  });
 }