底部导航无效参数,抖动

问题描述

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

错误

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,bgcolor: Colors.blue[200],);
      } 
    }


     // ignore: missing_return
     Widget _timeout(){
   //enable
      if(timeInText!="                 Time in") {
        return RoundedButton(icon: Icon(Icons.timer,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,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),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,)
            
      ),Container(
        child:Padding(padding: EdgeInsets.fromLTRB(10,70,child:Column(children: <Widget>[
          
          Textfield(text: "Welcome!",font: 27,fontcolor: Colors.blue[900],fontweight: FontWeight.bold,SizedBox(height:10),Textfield(text: getname,font: 20,Textfield(text: ""+getdesignation+" ",font: 17,fontcolor: Colors.blue[700],SizedBox(height:100,width: 30,Padding(padding: EdgeInsets.fromLTRB(30,20,15),child: _timein(),60),child:_timeout(),//logout button
        Padding(padding: EdgeInsets.fromLTRB(0,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,))],)
    )),)
    
    
 
     
      
    );
  }
}


错误提示

enter image description here

请帮助我做错的地方。

解决方法

这个错误似乎与 TextField(text: ... ) 不能是 null 但它可以是空字符串 "" 有关。

将您的字符串变量初始化为一些像 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. 
});
}