1

I have this JSON from my API in localhost

{"username":"coreadmin","password":"adminAdmin","name_display":"gm","role_id":1,"position":"admin","firstname":"gm","lastname":"gm","phone":"1233-123-1233","email":"test@admin.test","country":"none","dt_created":"2021-09-27T02:17:09.000Z","dt_updated":"2021-09-27T02:17:09.000Z"}

That data is displayed when I access it via web browser.

I attempted to get the data and display it via flutter, here's my code :

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

import '../models/users.dart' as users_model;

class FetchJson {      
  static Future<users_model.Users> fetchCredential(String username, String password) async {
    final response = await http.get(Uri.parse('http://localhost:7777/login?username=coreadmin&password=adminADMIN'));

    if (response.statusCode == 200) {
      return users_model.Users.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Failed to load users credentials: ' + response.statusCode.toString());
    }
  }
}

But there's no response.body while response.statusCode == 200 The error is

Error: FormatException: SyntaxError: Unexpected end of JSON input

James Z
  • 12,209
  • 10
  • 24
  • 44
Ray
  • 31
  • 5
  • Are you getting string in response, if not then you shouldn't use `jsonDecode` – Harshit Rastogi Oct 04 '21 at 11:12
  • you get respone.statusCode is 200 ? refer my answer [here](https://stackoverflow.com/a/68767696/13997210) – Ravindra S. Patil Oct 04 '21 at 11:16
  • I tried your JSON response string with jsonDecode() method. It doesn't gives an error. I think your users_model.Users.fromJson() method causes the error – rosh-dev851 Oct 04 '21 at 11:34
  • @HarshitRastogi it was a stringified JSON. But the response got nothing before the if clause. – Ray Oct 04 '21 at 13:38
  • @RavindraS.Patil Thank you, but I think we have a different case here, as your case is for POST method and my case is GET method. Kindly elaborate your answer below. – Ray Oct 04 '21 at 13:43
  • @rosh-dev I am literally following the guide here [link](https://flutter.dev/docs/cookbook/networking/fetch-data) and I also tried to make the method by generating it from [link](https://javiercbk.github.io/json_to_dart/) – Ray Oct 04 '21 at 13:54
  • Are you get data from API? – Ravindra S. Patil Oct 04 '21 at 14:33
  • @RavindraS.Patil Yes, it is a json. And the issue somehow solved while I did nothing. Thank you – Ray Oct 04 '21 at 15:45
  • If you get data from API refer my answer [here](https://stackoverflow.com/a/68709502/13997210) or [here](https://stackoverflow.com/a/68533647/13997210) or [here](https://stackoverflow.com/a/68594656/13997210) or [here](https://stackoverflow.com/a/69116765/13997210) hope it's helpful to you – Ravindra S. Patil Oct 04 '21 at 17:08

0 Answers0