Giacomo M
Giacomo M

Reputation: 4723

fetch json array from webservice

I am trying to fetch a json array from webservice.

The docs say:

_getIPAddress() {
  final url = 'https://httpbin.org/ip';
  HttpRequest.request(url).then((value) {
      print(json.decode(value.responseText)['origin']);
  }).catchError((error) => print(error));
}

If I use this code I get the error:

The method request is not defined for the class 'HttpRequest'

While If I try to import:

import 'dart:html';

I get this error:

Target of URI doesn't exist 'dart:html'

Upvotes: 0

Views: 61

Answers (1)

Snirjka
Snirjka

Reputation: 315

For http requests i recommend the http package.

Then after importing the http package you can use it like that for example:

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

_getIPAddress() async {
  final url = 'https://httpbin.org/ip';
  try {
    http.Response res = await http.get(url);
    print(json.decode(res.body));
  } catch(e) {
       print(e);    
     }     
}

Upvotes: 1

Related Questions