Fetch data from API calls in Flutter

Flutter uses the http package. To install the http package, add it to the dependencies’ section of our pubspec.yaml.

dependencies:
  flutter:
    sdk: flutter
  http: 

Flutter uses the dart:io core HTTP support client. To create an HTTP Client, import dart:io.

import 'dart:io';

The client supports the following HTTP operations: GET, POST, PUT, and DELETE.

final url = Uri.parse('https://httpbin.org/ip');
final httpClient = HttpClient();

Future getIPAddress() async {
  final request = await httpClient.getUrl(url);
  final response = await request.close();
  final responseBody = await response.transform(utf8.decoder).join();
  final String ip = jsonDecode(responseBody)['origin'];
  setState(() {
    _ipAddress = ip;
  });
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search