Home > Enterprise >  flutter http.post Error: Expected a value of type 'bool', but got one of type 'String
flutter http.post Error: Expected a value of type 'bool', but got one of type 'String

Time:08-11

I am building a authentication app in flutter with mysql as my database for the users. I used the flutter_login package. I tried to post using http.post but it gives me an error of Error: Expected a value of type 'bool', but got one of type 'String'.

Code Snippet:

Future<String?> _authUser(LoginData data) {
    return Future.delayed(loginTime).then((_) async {
      String apiurl = "http://jodielocal.local/cocosap/cocosaplogin2.php";
      print('Name: ${data.name}, Password: ${data.password}');

      var response = await http.post(Uri.parse(apiurl), body: {
        'username': data.name, //get the username text
        'password': data.password //get password text
      }).catchError((e) {
        print(e);
      });
      if (response.statusCode == 200) {
        var jsondata = json.decode(response.body);
        if (jsondata["error"] & jsondata["message"] ==
            "Your Password is Incorrect.") {
          return 'Password does not match';
        }
        if (jsondata["error"] & jsondata["message"] == "No username found.") {
          return 'Username not exists';
        } else {
          if (jsondata["success"]) {
            setState(() {
              addBoolToSF();
            });
            return Future<String?>(() {});
          }
        }
      } else {
        setState(() {
          showprogress = false; //don't show progress indicator
          error = true;
          errormsg = "Error during connecting to server.";
        });
      }
    });

VS Code indicate that the error is here: enter image description here

I am new to flutter and would appreciate any help. thank you

EDIT Entire Error:

    at Generator.next (<anonymous>)
    at http://localhost:64564/dart_sdk.js:40641:33
    at _RootZone.runUnary (http://localhost:64564/dart_sdk.js:40511:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:64564/dart_sdk.js:35438:29)
    at handleValueCallback (http://localhost:64564/dart_sdk.js:35999:49)
    at _Future._propagateToListeners (http://localhost:64564/dart_sdk.js:36037:17)
    at [_completeWithValue] (http://localhost:64564/dart_sdk.js:35872:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:64564/dart_sdk.js:35906:35)
    at Object._microtaskLoop (http://localhost:64564/dart_sdk.js:40778:13)
    at _startMicrotaskLoop (http://localhost:64564/dart_sdk.js:40784:13)
    at http://localhost:64564/dart_sdk.js:36261:9

CodePudding user response:

The condition needs to be added like this

if (jsondata["error"] == "Your Password is Incorrect" && jsondata["message"] ==
            "Your Password is Incorrect.") {
          return 'Password does not match';
        }

Please notice there are 2 && and each condition is written separately. Thats ehen it will return a bool value for condition check

  • Related