Flutter-Http Json响应

德士

我正在尝试颤抖,并且已经连接了一个http帖子以进行登录。

我创建了一个文件来处理我的http请求在文件中写入以下代码:

Map data;

Future<Map> logIn(email, password) async {

    String url = "...";
    Object userData = { "email": email, "password": password };
    var userDataBody = JSON.encode(userData);
    Map userHeader = {"Content-type": "application/json", "Accept": "application/json"};

    http.Response response = await http.post(
                Uri.encodeFull(url), 
                headers: userHeader, 
                body: userDataBody 
    );

    data = JSON.decode(response.body);
    print(data);
    return data;
}

Http返回以下数据:

{message: Login Successful, status: success}

上面的代码在有状态的小部件中使用

// imported the file above as userService
import 'services/user_services.dart' as userService;

// created a method for a button
void submitData() {
    final form = formKey.currentState;

    if (form.validate()) {  // no validations so far
      form.save();  // save email and password
      userService.logIn("$userEmail", "$userPassword");      // .then(JSON.decode(responseContent['message']));
    }
}

// button in container
new Container(
    padding: new EdgeInsets.all(20.0),
    child: new RaisedButton(
      onPressed: submitData,
      child: new Text('LogIn'),
    )
),

我的问题是,对于返回的json数据,我正在努力获取返回的状态(成功)/消息(成功登录),然后对它们执行我喜欢的任何事情。

贡特·佐赫鲍尔(GünterZöchbauer)

不确定问题是什么,但我想你想要的是

void submitData() async {
  ...
  var result = await userService.logIn("$userEmail", "$userPassword"); 
}

这样,您可以掌握结果。如果这正是您要寻找的,则无法从异步调用恢复同步。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章