0

i want to get data from my api web server. I tested on Postman the data is there, even when I try it on my browser the data is still there. but when i want to use the api in my flutter app why is the api empty.

postman tested enter image description here

my code to get api.

class DioClient {
  final Dio _dio = Dio(
    BaseOptions(
      baseUrl: 'https://spotify.test/api',
      contentType: "application/json",
      responseType: ResponseType.json,
    )
  );

  Future<HeaderList> getDatas() async {
    Response datas = await _dio.get('/load/songs');
    print('data = ${datas.data}');
    return HeaderList.fromJson(datas.data);
  }
}

my model

class HeaderList {
  final int total;
  final List<Artist> artist;
  final List<Song> songs;

  HeaderList({
    required this.total,
    required this.songs,
    required this.artist,
  });

  factory HeaderList.fromJson(Map<String, dynamic> json) => HeaderList(
    total: json['total data'],
    artist: List<Artist>.from(json['artist'].map((element) => Artist.fromJson(element))),
    songs: List<Song>.from(json['songs'].map((element) => Song.fromJson(element))),
  );
}

is there any error in my code or my laravel web project ?. because I'm still a beginner in laravel ?

  • Don't you get any output from `print('data = ${datas.data}');` ? – Dasun Tharanga Jun 29 '22 at 15:12
  • Are you getting the data in debug mode describe your problem because in release mode you should add internet permission in order to use http requests in android – Saffron-codes Jun 29 '22 at 15:16
  • In Postman you are sending form data, but in Dart you are asserting that the contents is JSON. – Richard Heap Jun 29 '22 at 15:59
  • And you are sending a GET in Dart, rather than a POST. – Richard Heap Jun 29 '22 at 16:02
  • The first thing I notice is postman is using HTTP:// but your code is using HTTPS:// Second I notice postman is POST, but dart is GET. Third, postman is body form-data, but dart is not posting and or not using name/password fields. – Cameron Jun 29 '22 at 17:16
  • sorry for late reply, it turns out that what makes api data return null is Failed host lookup: 'spotify.test' (OS Error: No address associated with hostname, errno = 7), I've replaced the post method with get. then what is the right solution for the above problem?, here https://stackoverflow.com/questions/54551198/how-to-solve-socketexception-failed-host-lookup-www-xyz-com-os-error-no -ad explained that in android manifest need to add permission, but in my case still error. – Alfian Rudiyanto Jun 30 '22 at 05:32

1 Answers1

1

You shouldn't need Dio for a simple request like this; just use package:http.

There are a couple of problems with your code. In Postman you are using the POST verb, but GET in Dart. You are also telling the server you are sending JSON, but it want (see your Postman request) form data.

Try this:

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

void main() async {
  final r = await http.post(
    Uri.https('spotify.test', 'api/load/songs'),
    body: {
      'password': 'open123456',
      'password_confirmation': 'open123456',
    },
  );

  print(r.statusCode);
  print(r.body);
}
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • thanks for the answer, apparently my problem lies in my web server ip. I just found out to access localhost on AVD using ip 10.0.2.2. Once again, thank you – Alfian Rudiyanto Jun 30 '22 at 09:53