【问题标题】:Failed assertion: line 65 pos 15: 'visible != null': is not true断言失败:第 65 行第 15 行:“可见!= null”:不正确
【发布时间】:2019-12-21 18:59:29
【问题描述】:

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

但我收到这样的错误:

I/flutter (15955):引发了另一个异常:'package:flutter/src/widgets/visibility.dart':断言失败:第 65 行 pos 15:'visible != null':不正确。

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

注意:查看/隐藏小部件的工作原理,只是“可见!= 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();
          },
        ),
      ],
    ));
  }
}

【问题讨论】:

    标签: android flutter visible


    【解决方案1】:

    仅针对那些可能有相同错误的人:断言失败:第 65 行 pos 15: 'visible != null': is not true 即使在 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;
      });
     }
    

    【讨论】:

      【解决方案2】:

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

      只需更改此行:

      bool menuAju;
      

      到这里:

      bool menuAju = false;
      

      【讨论】:

        猜你喜欢
        • 2019-06-24
        • 2021-05-12
        • 1970-01-01
        • 2019-05-24
        • 2020-08-31
        • 2021-06-18
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        相关资源
        最近更新 更多