Reputation: 1838
I am have created a file where I am creating bottom navigation and drawer, when i run the app after login it's sending me this error.
ERROR:
The following ArgumentError was thrown building EmployeeDashboard(dirty, state: _EmployeeDashboardState#e749a):
Invalid argument(s)
but works fine, it is not showing any red screen on emulator, but when i do run the app on my phone it's showing red screen with this error "Invalid Arguments"
here is my code
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'),
),
],
),
);
}
}
--------------------------Update-------------------------------------
here is EmployeeDashboard file
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,
),
),
))],)
)),
],
)
);
}
}
snap of error
Please help where i am doing wrong.
Upvotes: 1
Views: 970
Reputation: 1658
This error seems to be related to the TextField(text: ... )
cannot be null
but it can be the Empty string ""
.
Initializing your string variables to some dummy initialization like getname="initStringForDebuggin"
seems to fix this issue. But the issue is not completely gone.
You should probably look into the futureBuilder()
Widget.
https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
and probably rewrite your code to first render after the future has completed. https://dart.dev/codelabs/async-await
To my knowledge it is bad form to have a setState() in a future, like you have in _userDetails() instead you should do something like this.
_userDetails().then((SOMERETURNVALUE) {
setState(() {
// Refresh screen.
});
}
Upvotes: 1