Flutter [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected character (at character 1)

2,883

As i can see there is nothing wrong with the code. Either you are accessing a wrong URL or there's something wrong with the web service. Please ensure the URL and see if you are accessing the correct URL.

Try to hit this api in Postman and see the response you are getting. You will get better understanding.

Share:
2,883
Davrick
Author by

Davrick

Updated on December 27, 2022

Comments

  • Davrick
    Davrick over 1 year

    In my application I am trying to login user through API, but I am getting the following error.

    E/flutter (14103): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected character (at character 1)
    E/flutter (14103): <!DOCTYPE html>
    E/flutter (14103): ^
    E/flutter (14103): 
    E/flutter (14103): #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
    E/flutter (14103): #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1271:9)
    E/flutter (14103): #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:936:22)
    E/flutter (14103): #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
    E/flutter (14103): #4      JsonDecoder.convert (dart:convert/json.dart:505:36)
    E/flutter (14103): #5      JsonCodec.decode (dart:convert/json.dart:156:41)
    E/flutter (14103): #6      jsonDecode (dart:convert/json.dart:96:10)
    E/flutter (14103): #7      FormBloc.login (package:curtain/blocs/form_bloc.dart:74:18)
    

    In my previous version of code everything was working successfully. After I did some modifications (including adding validators) is my code not working. Below you can see my http request:

    static Future<dynamic> loginUser(String username, String password) async {
        final result = await http.post('$baseURL/en/users/login/', headers: {
          'Accept': 'application/json',
        }, body: {
          'username': username,
          'password': password,
        });
    
        return result?.body;
      }
    

    And this is how I am calling it:

    dynamic login(BuildContext context) async {
        final res = await AuthService.loginUser(_username.value, _password.value);
        final data = jsonDecode(res) as Map<String, dynamic>;
    
        if (data['status'] != 200) {
          addError(data['message']);
        } else {
          Navigator.of(context).push(
            CupertinoPageRoute(
              builder: (context) => FeedScreen(),
            ),
          );
          return res;
        }
    

    let me know if you need to see how my json response should be

  • Davrick
    Davrick about 3 years
    That is the thing. It is working in Postman. It is working when I am logging in through the URL itself as well. But here errors.
  • Davrick
    Davrick about 3 years
    Could it be because I am not using Strings for username and password, but BehaviorSubject<String>() ??
  • Gaurav Singh
    Gaurav Singh about 3 years
    Before http.post() ensure the data which you are passing as values i.e username and password in Log
  • Davrick
    Davrick about 3 years
    Yeah, I printed the data before http.post(), they are correct. However, it is giving the above error just after those printed values
  • Davrick
    Davrick about 3 years
    Yes, I found it. When I defined the baseURL I added slash (/) in the end. And here when I was calling it http.post('$baseURL/en/users/login/') I added another slash, which was not needed. Thank you.