Flutter 类型 'String' 不是类型 'FutureOr<Model>' 的子类型,没有 Key 的响应 Json

Flutter type 'String' is not subtype of type 'FutureOr<Model>' , Response Json Without Key

我正在尝试这样调用服务,我认为这里有问题,因为“结果”return Future 无法分配给 String,或者如何将 Future 解析为 String? 和 MyApi return Json 没有密钥像这样的东西

body

ReturnStatus

所以我尝试将响应直接存储到用户变量中,但仍然无法正常工作

UI (Button Login)

login.loginProcess
    ? Center(child: CircularProgressIndicator())
    : RaisedButton(
        color: myPrimaryColor,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8)),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Login",
                style: TextStyle(
                    color: Colors.black,
                    fontFamily: "NunitoSansBold")),
          ],
        ),
        padding:
            EdgeInsets.only(top: 16.0, bottom: 16.0),
        onPressed: () {
          print("clicked Button Login");
          **login.authenticationUser(context);**
        },
      ),

Service

class AuthService {
  Future<User> authenticateUser(String id, String password) async {
    var user;
    try {
      final resAuth = await http.post(
        "${BaseUrl.auth}api/AuthenticateUser",
        body: {"login": id, "password": password},
      );
      if (resAuth.statusCode == 200) {
        user = resAuth.body;
      }
    } catch (e) {
      return user;
    }
    return user;
  }
}

Provider with ChangeNotifier (For handling the business logic and stuf)

authenticationUser(BuildContext context) async {
  if (_controllerEmail.text != "" && _controllerPassword.text != "") {
    loginProcess = true;
    final ioc = new HttpClient();
    ioc.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
    //TODO StoredSharedPreference
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    try {
      AuthService authService = new AuthService();
      var result = authService.authenticateUser(
          _controllerEmail.text, _controllerPassword.text);
      showSnackBar(result);
      _preferences.setString("status", "seen");
    } catch (e) {
      print(e);
      showSnackBar(result.toString());
    }
    loginProcess = false;
  } else {
    autoValidate = true;
  }
} 

Model

class User {
  String status;
  String id;
  String userName;

  User({
    this.status,
    this.id,
    this.userName,
  });

  factory User.fromJson(Map<String, dynamic> json) =>
      User(status: json["status"], id: json["id"], userName: json["userName"]);

  Map<String, dynamic> toJson() =>
      {"status": status, "id": id, "userName": userName};
}

=======UPDATE , 更改方法 authenticationUser (添加 await)=======

Service

class AuthService {
  Future<User> authenticateUser(String id, String password) async {
    var user;
    try {
      final resAuth = await http.post(
        "${BaseUrl.auth}api/AuthenticateUser",
        body: {"login": id, "password": password},
      );
      if (resAuth.statusCode == 200) {
        user = resAuth.body;
      }
    } catch (e) {
      return user;
      // throw Exception(e.toString());
    }
    return user;
  }
}

Provider

authenticationUser(BuildContext context) async {
    if (_controllerEmail.text != "" && _controllerPassword.text != "") {
      loginProcess = true;
      final ioc = new HttpClient();
      ioc.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
      //TODO StoredSharedPreference
      SharedPreferences _preferences = await SharedPreferences.getInstance();
      try {
        AuthService authService = new AuthService();
        var result = await authService.authenticateUser(
            _controllerEmail.text, _controllerPassword.text);
        _preferences.setString("status", "seen");
        showSnackBar(result.toString());
      } catch (e) {
        print(e);
        /* showSnackBar(e.toString());*/
      }
      loginProcess = false;
    } else {
      autoValidate = true;
    }
  }

赶上(e){ 打印(e);

e 的值为 String' is not a subtype of type 'FutureOr<User>'

==从 resAuth 更新添加图像值==

resAuthValue

您可能需要添加 await

var result = await authService.authenticateUser(
          _controllerEmail.text, _controllerPassword.text);

编辑

将您的 authenticateUser 方法更改为此

Future authenticateUser(String id, String password) async {
    var user;
    try {
      final resAuth = await http.post(
        "${BaseUrl.auth}api/AuthenticateUser",
        body: {"login": id, "password": password},
      );
      if (resAuth.statusCode == 200) {
        return resAuth.body;
      }
    } catch (e) {
     return false;
    }
    return false;
  }

更改供应商代码如下

 try {
        AuthService authService = new AuthService();
        var result = await authService.authenticateUser(
            _controllerEmail.text, _controllerPassword.text);
        _preferences.setString("status", "seen");
        if(result!=false)
        showSnackBar(result.toString());
      } catch (e) {
        print(e);
        /* showSnackBar(e.toString());*/
      }