1
votes

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

1

1 Answers

1
votes

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.