首页 > 解决方案 > 底部导航上的无效参数,颤动

问题描述

我创建了一个文件,我在其中创建底部导航和抽屉,当我登录后运行应用程序时,它向我发送此错误。

错误:

The following ArgumentError was thrown building EmployeeDashboard(dirty, state: _EmployeeDashboardState#e749a):
Invalid argument(s)

但工作正常,它没有在模拟器上显示任何红屏,但是当我在手机上运行该应用程序时,它显示红屏并出现此错误“无效参数”

这是我的代码

class EmployeeNavigation extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return EmployeeNavigationState();
  }
}
String getname;
 String getemail;
 String getdesignation;

class EmployeeNavigationState extends State<EmployeeNavigation> {


  int _selectedTab = 0;
  final _pageOptions = [
  EmployeeDashboard(),  // It's pointing over here
  Profile(),
    //SearchPage(),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          
    ),
      drawer: Emp_DrawerCode(),
      body: _pageOptions[_selectedTab],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedTab,
        onTap: (int index) {
          setState(() {
            _selectedTab = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text('Profile'),
          ),
        ],
      ),
    );
  }
  
}


- - - - - - - - - - - - - 更新 - - - - - - - - - - - - --------------

这是EmployeeDashboard文件


class EmployeeDashboard extends StatefulWidget {
  @override
  _EmployeeDashboardState createState() => _EmployeeDashboardState();
}
 

class _EmployeeDashboardState extends State<EmployeeDashboard> {
  String getTime;
  bool valuefirst = false;
  String getname;
  String getemail;
  String getdesignation;
  bool getTimeInStatus;
  String getaccesstoken;
  
  @override
    void initState() {

      _userDetails();
      _getTime();
      
    }

 _userDetails() async{
    SharedPreferences myPrefs=await SharedPreferences.getInstance();
    setState(() {
          getname=myPrefs.getString('name');
          getdesignation=myPrefs.getString('designation');
          getTimeInStatus=myPrefs.getBool('timeInStatus');
          getaccesstoken=myPrefs.getString('accesstoken');
        }); 
  }
  var localhostUrlTimeIn="http://192.168.1.105:8000/TimeIn";
  var localhostUrlTimeOut="http://192.168.1.105:8000/TimeOut";

  calltimeInApi() async {

    Dio dio=new Dio();
    var data={
      'username': getname,
      'token': getaccesstoken
    };
    await dio
    .post(localhostUrlTimeIn,data: json.encode(data))
      .then((onResponse)async {
       
        print(onResponse.headers);
        print(onResponse.statusCode);
        print(onResponse.data);
      }).catchError((onerror){
        print(onerror.toString());
        //showAlertDialog(context);
    });

    
  }
  calltimeOutApi() async {

    Dio dio=new Dio();
    var data={
      'username': getname,
      'token': getaccesstoken
    };
    await dio
    .post(localhostUrlTimeOut,data: json.encode(data))
      .then((onResponse)async {
       
        print(onResponse.headers);
        print(onResponse.statusCode);
        print(onResponse.data);
      }).catchError((onerror){
        print(onerror.toString());
        //showAlertDialog(context);
    }); 
  }
  
  void _getTime() {
    final String formattedDateTime =
        DateFormat('kk:mm:ss').format(DateTime.now()).toString();
    setState(() {
      getTime = formattedDateTime;
     print(getTime[0]);
    });
  }
  
  String _formatDateTime(DateTime dateTime) {
    return DateFormat('MM/dd/yyyy hh:mm:ss').format(dateTime);
  }
  
  String timeInText="                 Time in";
  String timeOutText="                Time out";

  bool timeInButtonPressed=false;
  bool timeOutButtonPressed=false;

  // ignore: missing_return
  Widget _timein() {
   //enable
    if(getTimeInStatus==false) {
      return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeInText,
              bgcolor: timeInButtonPressed ?Colors.blue[200]:Colors.blue[500],
              press: () {
                _getTime();
                setState(() {
                timeInText="                 "+getTime;
                });
                calltimeInApi();
                timeOutButtonPressed=true;
                
                
              });
    }
    //disable
      else if(getTimeInStatus==true) {
      return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeInText,
              bgcolor: Colors.blue[200],
              
              );
      } 
    }


     // ignore: missing_return
     Widget _timeout(){
   //enable
      if(timeInText!="                 Time in") {
        return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeOutText,
              bgcolor: timeOutButtonPressed ?Colors.blue[500]:Colors.blue[200],
              press: () {
                _getTime();
                setState(() {
                timeOutText="                 "+getTime;    
                });
                calltimeOutApi();
                
        });

      }
      //disable
      else if(timeOutButtonPressed==false){
        return RoundedButton(icon: Icon(Icons.timer,color: Colors.white),
              text:timeOutText,
              bgcolor: Colors.blue[200]);
      }
 }
 
  @override
 Widget build(BuildContext context) {
  return new Scaffold(
    body:
    Stack(
     children:[
       Container(
          color: Colors.white,
            child: 
                Row(  
                  children: <Widget>[  
                    SizedBox(width: 30,),  
                    Text('Working from home?',style: TextStyle(fontSize: 20.0), ),  
                    Checkbox(  
                      checkColor: Colors.greenAccent,  
                      activeColor: Colors.blue, 
                      value: this.valuefirst,  
                      onChanged: (bool value) {  
                        setState(() {  
                          this.valuefirst = value;  
                        });  
                      },   
                    ), 
                  ], 
                ),
        ),
        Container(
          child:Padding(padding: EdgeInsets.fromLTRB(32, 40, 0, 0),
          child: AnalogClock(
            decoration: BoxDecoration(
                border: Border.all(width: 4.0, color: Colors.blue[900]),
                color: Colors.white,
                shape: BoxShape.circle),
            width: 130.0,
            height: 170.0,
            isLive: true,
            hourHandColor: Colors.blue[900],
            minuteHandColor: Colors.blue[900],
            showSecondHand: true,
            numberColor: Colors.blue[900],
            showNumbers: true,
            textScaleFactor: 1.9,
            showTicks: true,
            showDigitalClock: true,
            digitalClockColor: Colors.blue[900],
            datetime: DateTime(2020, 8, 4, 9, 11, 0),
            
            ),
          )
            
      ),
      Container(
        child:Padding(padding: EdgeInsets.fromLTRB(10, 70, 0, 0),
        child:Column(children: <Widget>[
          
          Textfield(text: "Welcome!",font: 27,fontcolor: Colors.blue[900],fontweight: FontWeight.bold,),
          SizedBox(height:10),
          Textfield(text: getname,font: 20,fontcolor: Colors.blue[900],fontweight: FontWeight.bold,),
          Textfield(text: ""+getdesignation+" ",font: 17,fontcolor: Colors.blue[700],fontweight: FontWeight.bold,),
          SizedBox(height:100,width: 30,),
          Padding(padding: EdgeInsets.fromLTRB(30, 20, 40,15),
          child: _timein(),),
          Padding(padding: EdgeInsets.fromLTRB(30, 0, 40,60),
          child:_timeout(),
         ),
        

        //logout button
        Padding(padding: EdgeInsets.fromLTRB(0, 0, 20, 20),
        child:SizedBox(
          height: 50.0,
          width: 400.0,
          child:FlatButton(
         onPressed: (){
           Navigator.push(context, MaterialPageRoute(builder: (context)=>Login()));
         },
         padding: EdgeInsets.all(0.0),
         child: 
          Image.asset(
            "assets/image/logout.jpeg",
            fit: BoxFit.contain,
          ),
          
        ),
      ))],)
    )),
   
     
   ],
   )
    
    
 
     
      
    );
  }
}


错误捕捉

在此处输入图像描述

请帮助我做错了什么。

标签: flutterbottomnavigationviewinvalid-argument

解决方案


此错误似乎与TextField(text: ... )cannot benull但它可以是 Empty string相关""

将您的字符串变量初始化为一些虚拟初始化getname="initStringForDebuggin"似乎可以解决此问题。但问题并没有完全消失。

您可能应该查看futureBuilder()小部件。 https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

并且可能会在未来完成后重写您的代码以首次渲染。 https://dart.dev/codelabs/async-await

据我所知,将来有一个 setState() 是不好的形式,就像你在 _userDetails() 中一样,你应该做这样的事情。

_userDetails().then((SOMERETURNVALUE) { 
setState(() {
// Refresh screen. 
});
}

推荐阅读