HTTP Requests

This article shows how to call HTTP requests.

Download source code

Dependencies

Add http package to pubspec.yaml

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  http: ^0.12.0+1

WebUtils

Create get / post / put / delete methods

// utils/webutils.dart

import 'dart:async';

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

class WebUtils {

  static WebUtils _instance = new WebUtils.internal();
  WebUtils.internal();
  factory WebUtils() => _instance;

  Future<dynamic> get(String url, {Map headers}) async {
    return http.get(url, headers: headers).then((http.Response response) {
      return response;
    });
  }

  Future<dynamic> post(String url, {Map headers, body, encoding}) async {
    return http
        .post(url, body: body, headers: headers, encoding: encoding)
        .then((http.Response response) {
      return response;
    });
  }

  Future<dynamic> put(String url, {Map headers, body, encoding}) async {
    return http
        .put(url, body: body, headers: headers, encoding: encoding)
        .then((http.Response response) {
      return response;
    });
  }

    Future<dynamic> delete(String url, {Map headers}) async {
    return http
        .delete(url, headers: headers)
        .then((http.Response response) {
      return response;
    });
  }
}

Call http

Get

  static Future<List<String>> getMethod() async {
    WebUtils _webUtils = new WebUtils();
    String _url = 'http://api.url';

    // basic authentification if needed
    String _username = 'username';
    String _password = 'password';
    String basicAuth =
        'Basic ' + base64Encode(utf8.encode('$_username:$_password'));
    Map<String, String> _headers = {HttpHeaders.authorizationHeader: basicAuth};

    var _result =
        await _webUtils.get(_url, headers: _headers).then((dynamic res) {
      return res;
    });

    if (_result.statusCode != 200) {
      // Do Something
      // Map s = json.decode(_result.body);
      // String error = s['message'];
      return null;
    }

    List decoded = json.decode(_result.body);

    List<String> returnList = new List<String>();

    for (int i = 0; i < decoded.length; i++) {
      returnList.add(decoded[i]["text"]);
    }

    return returnList;
  }

Post

  static Future<double> postMethod(dynamic body) async {
    WebUtils _webUtils = new WebUtils();
    String _url = 'http://api.url';

    // basic authentification if needed
    String _username = 'username';
    String _password = 'password';
    String basicAuth =
        'Basic ' + base64Encode(utf8.encode('$_username:$_password'));

    Map<String, String> _headers = {
      'authorization': basicAuth,
      'Content-Type': 'application/json'
    };

    http.Response _result = await _webUtils
        .post(_url, headers: _headers, body: body)
        .then((dynamic res) {
      return res;
    });

    if (_result.statusCode != 200) {
      // Do Something
      // Map s = json.decode(_result.body);
      // String error = s['message'];
      return -1;
    }

    return double.parse(_result.body);
  }

Put

  // body = jsonString content
  static Future<bool> putMethod(dynamic body) async {
    WebUtils _webUtils = new WebUtils();
    String _url = 'http://api.url';

    // basic authentification if needed
    String _username = 'username';
    String _password = 'password';
    String basicAuth =
        'Basic ' + base64Encode(utf8.encode('$_username:$_password'));
    Map<String, String> _headers = {
      'authorization': basicAuth,
      'Content-Type': 'application/json'
    };

    http.Response _result = await _webUtils
        .put(_url, headers: _headers, body: body)
        .then((dynamic res) {
      return res;
    });

    if (_result.statusCode != 200) {
      // Do Something
      // Map s = json.decode(_result.body);
      // String error = s['message'];
      return false;
    }

    return true;
  }

Delete

  static Future<bool> deleteMethod(String id) async {
    WebUtils _webUtils = new WebUtils();
    String _url = 'http://api.url';

    // basic authentification if needed
    String _username = 'username';
    String _password = 'password';
    String basicAuth =
        'Basic ' + base64Encode(utf8.encode('$_username:$_password'));

    Map<String, String> _headers = {
      'authorization': basicAuth,
      'Content-Type': 'application/json'
    };
    http.Response _result =
        await _webUtils.delete(_url, headers: _headers).then((dynamic res) {
      return res;
    });

    if (_result.statusCode != 200) {
      // Do Something
      // Map s = json.decode(_result.body);
      // String error = s['message'];
      return false;
    }

    return true;
  }