[英]Flutter Dart "Expected an identifier" & "Expected to find ')'"
I'm trying to compare an object to a string the user has inputted.我正在尝试将 object 与用户输入的字符串进行比较。 the object is from a json api response I've mapped.
object 来自我映射的 json api 响应。 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这是 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编辑 1
Diving Deep into this, when the.then() is created it goes like this,深入研究,当 the.then() 被创建时,它是这样的,
onPressed: (){
fetchUser().then((value) => null);
},
here is the place you made the mistake.这是你犯错的地方。 => is pointing to a function.
=> 指向 function。 so when you put a functions there it should be just the name of the function like this,
因此,当您将函数放在那里时,它应该只是 function 的名称,如下所示,
onPressed: (){
fetchUser().then((value) => myfunctions());
},
but if you write the function there it should be like this,但是如果你写 function 应该是这样的,
onPressed: (){
fetchUser().then((value){
//your code
});
},
Change .then((value) => <your code>
更改
.then((value) => <your code>
to .then((value) { <your code> }
到
.then((value) { <your code> }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.