0

I have a problem with my login in Flutter. When I click the login button, a pop up token information appears. When I click login it should go to the home page.

Popup: "Informasi. FormatException: Unexpected character (at character 1)", token is shown

This is my source code.

login_page.dart

void _validateInputs() {
    if (_formKey.currentState!.validate()) {
      //If all data are correct then save data to out variables
      _formKey.currentState!.save();
      doLogin(txtEditEmail.text, txtEditPwd.text);
    }
  }

  doLogin(email, password) async {
    final GlobalKey<State> _keyLoader = GlobalKey<State>();
    Dialogs.loading(context, _keyLoader, "Proses ...");

    try {

      String? token;
      SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      token = sharedPreferences.getString('token');
      final response = await http.post(
          Uri.parse("http://192.168.100.211:8080/login"),
          headers: {
            'Content-Type': 'application/json; charset=UTF-8',
            'Accept': 'application/json',
            'Authorization': 'Bearer $token',},

          body: json.encode({
            "email": email,
            "password": password,
          }));

      final output = json.decode(response.body);
      if (response.statusCode == 200) {
        Navigator.of(_keyLoader.currentContext!, rootNavigator: false).pop();
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
              content: Text(
            output['message'],
            style: const TextStyle(fontSize: 16),
          )),
        );

        if (output['success'] == true) {
          saveSession(email);
        }
        //debugPrint(output['message']);
      } else {
        Navigator.of(_keyLoader.currentContext!, rootNavigator: false).pop();
        //debugPrint(output['message']);
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
              content: Text(
            output.toString(),
            style: const TextStyle(fontSize: 16),
          )),
        );
      }
    } catch (e) {
      Navigator.of(_keyLoader.currentContext!, rootNavigator: false).pop();
      Dialogs.popUp(context, '$e');
      debugPrint('$e');
    }
  }

  

Can friends help solve my problem?

Laurel
  • 5,965
  • 14
  • 31
  • 57
Lussy Ika
  • 11
  • 4
  • Can u add more info about response from http.post ? – manhtuan21 May 30 '22 at 03:55
  • Try this `final output = await json.decode(json.encode(response.body));` – Miftakhul Arzak May 30 '22 at 04:08
  • Does this answer your question? [Flutter FormatException: Unexpected character (at character 1)](https://stackoverflow.com/questions/55671441/flutter-formatexception-unexpected-character-at-character-1) – Miftakhul Arzak May 30 '22 at 04:10
  • @MiftakhulArzak I have tried but there is an error like this : Error on line 1, column 32: Invalid media type: expected no more input. ╷ 1 │ application/json; charset=UTF-8, text/html; charset=UTF-8 – Lussy Ika May 30 '22 at 05:56
  • Can you print `response.body`? Let me know. – Miftakhul Arzak May 30 '22 at 06:30
  • @MiftakhulArzak error on screen like this : Error on line 1, column 32: Invalid media type: expected no more input. ╷ 1 │ application/json; charset=UTF-8, text/html; charset=UTF-8 – Lussy Ika May 30 '22 at 06:39

1 Answers1

1

post body like this

 body: {
                "email": email,
                "password": password,
              } 

Instead of

body: json.encode({
            "email": email,
            "password": password,
          })
Hammad Ali
  • 328
  • 2
  • 8