Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Dart "Expected an identifier" & "Expected to find ')'"

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");}
}
``

like image 520
limitlessbritt Avatar asked Sep 04 '25 02:09

limitlessbritt


2 Answers

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
  });
},
like image 132
Srilal Sachintha Avatar answered Sep 07 '25 15:09

Srilal Sachintha


Change .then((value) => <your code>

to .then((value) { <your code> }

like image 23
Sonu kumar Avatar answered Sep 07 '25 14:09

Sonu kumar