异步不等待完成

时间:2021-05-16 06:34:35

标签: flutter dart async-await

我正在尝试使以下方法等待完成,但它在“print(user)”行之前停止:

Future<ModelUser> createUserWithEmailAndPassword({
    @required String email,
    @required String password,
  }) async {
    assert(email != null);
    assert(password != null);

    var user = new ModelUser(email: email, password: password);

    final response = await post(Uri.http(_options.baseUrl, '/createAccount'),
        headers: _headers, body: jsonEncode(user.toJson()));

    if (response.statusCode == 200) {
      Map<String, dynamic> userMap = jsonDecode(response.body);
      var user = ModelUser.fromJson(userMap);
      print(user);
      return ModelUser.fromJson(jsonDecode(response.body));
    }

    return null;
  }

我从另一个方法调用它,使用 await:

Future<ModelUser> registerWithEmailAndPassword(
      String email, String password) async {
    try {
      return _user(await _auth.createUserWithEmailAndPassword(
          email: email, password: password));
    } catch (e) {
      return null;
    }
  }

0 个答案:

没有答案