Vineeth Vijayan
Vineeth Vijayan

Reputation: 1329

How to do an ajax get in flutter?

I am trying to do an ajax post in flutter ajax post

This is my code.

get() async {
      var url = 'https://jsonplaceholder.typicode.com/users';
      var httpClient = new HttpClient();

      String result;
      try {
        var request = await httpClient.getUrl(Uri.parse(url));
        var response = await request.close();
        if (response.statusCode == HttpStatus.OK) {
          var jsonString = await response.transform(utf8.decoder).join();
          var data = json.decode(jsonString);
          result = data;
        } else {
          result =
              'Error getting IP address:\nHttp status ${response.statusCode}';
        }
      } catch (exception) {
        result = 'Failed getting IP address';
      }

      print(result);
    }

When I try with https://httpbin.org/ip it works fine. but with https://jsonplaceholder.typicode.com/users it fail, I am very new to flutter and help much appreciated.

I printed out the exception it was throwing.

type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type
'_HttpIncoming' where
_BoundSinkStream is from dart:async List is from dart:core int is from dart:core List is from dart:core int is from dart:core
_HttpIncoming is from dart:_http List is from dart:core int is from dart:core

Couldn't make anything out of it.

When changing to String result to List result = []

type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where
_BoundSinkStream is from dart:async
List is from dart:core
int is from dart:core
List is from dart:core
int is from dart:core
_HttpIncoming is from dart:_http
List is from dart:core
int is from dart:core

Updated the code with more debug info :

getData() async {
    var url = 'https://jsonplaceholder.typicode.com/users';
    var httpClient = new HttpClient();

    List result = [];
    try {
      print('0');
      var request = await httpClient.getUrl(Uri.parse(url));
      print('1');
      var response = await request.close();
      print('2');
      if (response.statusCode == HttpStatus.OK) {
        print('3');
        var jsonString = await response.transform(utf8.decoder).join();
        print('4');
        var data = json.decode(jsonString);
        print('5');
        result = data;
        print('6');
      } else {
        // result = 'Error getting IP address:\nHttp status ${response.statusCode}';
      }
    } catch (exception) {
      // result = 'Failed getting IP address';
      print('7');
      print(exception);
      print('8');
    }

    print(result);
  }

Exception :

0
1
2
3
7
type '_BoundSinkStream<dynamic, List<int>>' is not a subtype of type '_HttpIncoming' where
_BoundSinkStream is from dart:async
List is from dart:core
int is from dart:core
List is from dart:core
int is from dart:core
_HttpIncoming is from dart:_http
List is from dart:core
int is from dart:core
8
[]

Flutter Version

Flutter 0.1.5 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 3ea4d06340 (5 weeks ago) • 2018-02-22 11:12:39 -0800
Engine • revision ead227f118
Tools • Dart 2.0.0-dev.28.0.flutter-0b4f01f759

True it was old I updated flutter

Flutter 0.2.3 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 5a58b36e36 (3 weeks ago) • 2018-03-13 13:20:13 -0700
Engine • revision e61bb9ac3a
Tools • Dart 2.0.0-dev.35.flutter-290c576264

Now I have a new error.

0
1
2
3
4
5
7
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List' where
_InternalLinkedHashMap is from dart:collection
String is from dart:core
List is from dart:core
8

I updated the code from a GitHub post

getData() async {
    print("0");
    var response = await http.get(
        Uri.encodeFull("https://jsonplaceholder.typicode.com/users"),
        headers: {"Accept": "application/json"});
    print("1");
    data = json.decode(response.body);
    print("2");
    print(data[1]["name"]);
    print("3");
    // print(response.body);
    print("end");
  }

And this is working.

Upvotes: 0

Views: 12382

Answers (2)

JLouage
JLouage

Reputation: 269

I don't know what http package you are using.

you can use this http package

and it work with the below code with both url

var url = 'https://jsonplaceholder.typicode.com/users';
//var url = "https://httpbin.org/ip";
http.get(url)
    .then((response) {
  print("Response status: ${response.statusCode}");
  print("Response body: ${response.body}");
});

Upvotes: 3

Yash Doshi
Yash Doshi

Reputation: 111

To decode JSON Array you need an List of string, try changing

String result;

with

List result = [];

Upvotes: 2

Related Questions