I'm trying to compare an object to a string the user has inputted. the object is from a json api response I've mapped. I get the error on "if":
MaterialButton(
child: Text("Connect"),
textColor: Colors.black,
onPressed: (){
fetchUser().then((user)=> (if user.username == username){
return Get.toNamed('/home');
});
},
color: Colors.grey[350],
)
here is the function
Future <User>fetchUser() async{
var authresponse = await http.get(userCall);
if (authresponse.statusCode == 200){
var jsondata = jsonDecode(authresponse.body);
final data = apicallFromJson(jsondata);
var user = data.subsonicResponse.user;
return user;
}else{
throw Exception("Unable to connect to server, try again");}
}
``
Looks like its a simple syntax error. here i corrected your code.
MaterialButton(
child: Text("Connect"),
textColor: Colors.black,
onPressed: (){
fetchUser().then((user){
if(user.username == username){
return Get.toNamed('/home');
}
});
},
color: Colors.grey[350],
)
EDIT 1
Diving Deep into this, when the .then() is created it goes like this,
onPressed: (){
fetchUser().then((value) => null);
},
here is the place you made the mistake. => is pointing to a function. so when you put a functions there it should be just the name of the function like this,
onPressed: (){
fetchUser().then((value) => myfunctions());
},
but if you write the function there it should be like this,
onPressed: (){
fetchUser().then((value){
//your code
});
},
Change .then((value) => <your code>
to .then((value) { <your code> }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With