【问题标题】:Flutter - Http Json ResponseFlutter - Http Json 响应
【发布时间】:2018-08-28 02:15:42
【问题描述】:

我正在试用flutter,并且我已经连接了http post 用于登录。

我创建了一个文件来处理我的http request。在文件中写了如下代码:

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}

上面的编码用在stateful widget:

// 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 数据,我正在努力获取返回的状态(成功)/消息(登录成功)并用它们做任何我喜欢的事情。

【问题讨论】:

    标签: json http-post flutter


    【解决方案1】:

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

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

    这样您就可以掌握结果。 如果这是您要查找的内容,则无法从异步调用中恢复同步。

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2021-02-27
      • 2021-02-07
      • 2021-02-04
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 2021-09-10
      相关资源
      最近更新 更多