【问题标题】:Flutter Dart "Expected an identifier" & "Expected to find ')'"Flutter Dart“期望一个标识符”和“期望找到')'”
【发布时间】:2020-09-25 01:58:06
【问题描述】:

我正在尝试将对象与用户输入的字符串进行比较。该对象来自我映射的 json api 响应。 我收到“如果”的错误:

                    MaterialButton(
                      child: Text("Connect"),
                      textColor: Colors.black,
                      onPressed: (){
                        fetchUser().then((user)=> (if user.username == username){
                          return Get.toNamed('/home');
                        });
                      },
                      color: Colors.grey[350],
                    )

这里是函数

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

【问题讨论】:

    标签: json rest flutter dart


    【解决方案1】:

    看起来这是一个简单的语法错误。我在这里更正了您的代码。

    MaterialButton(
          child: Text("Connect"),
          textColor: Colors.black,
          onPressed: (){
             fetchUser().then((user){
                if(user.username == username){
                     return Get.toNamed('/home');
                }
             });
          },
          color: Colors.grey[350],
         )
    

    编辑 1

    深入研究,当创建 .then() 时,它是这样的,

     onPressed: (){
        fetchUser().then((value) => null);
     },
    

    这里是你犯错的地方。 => 指向一个函数。因此,当您将函数放在那里时,它应该只是这样的函数名称,

    onPressed: (){
         fetchUser().then((value) => myfunctions());
    },
    

    但是如果你在那里写函数应该是这样的,

    onPressed: (){
      fetchUser().then((value){
         //your code
      });
    },
    

    【讨论】:

    • 感谢您的解释,这对您有很大帮助!
    【解决方案2】:

    更改 .then((value) =&gt; &lt;your code&gt;

    .then((value) { &lt;your code&gt; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-31
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2018-05-09
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多