We use the requests that come with the Dart language so that we can use them on the server side as well

Simple configuration, MOCK data support, post support

http.dart

import 'dart:io';
import 'dart:async';
import 'dart:convert';
import './config.dart';

Future<String> HTTP(String path, Map<String, dynamic> queryParameters) async{
  return new Future(() async{
    if(CONFIG.HTTP_MOCK){
      print('emulated request only, request path: $path');
      switch(path){
        case 'getxhsalxbsahomexsajkxblist':{
          String response = ' '' '' ';
          return response;
        }
        case 'getxsavioxsachaterhgjkgliost':{
          String response = ' '' '' ';
          return response;
        }
        case 'get_vxsaeoxaschterxsaxsano':{
          String response = ' '' '' ';
          return response;
        }
        default :{
          print('FAILED to match API address');
          return ' ';
        }
      }
    }
    HttpClient httpClient = new HttpClient();
    HttpClientRequest request;
    
    // POST
    request = await httpClient.postUrl(
      // queryParameters: queryParameters
      Uri(scheme: "http", host: CONFIG.BaseHost, path: path, port: 9001)
    );
    request.headers.add('Accept'.'application/json, text/plain, */*');
    request.headers.add('Content-Type'.'application/json; charset=UTF-8');
    String payload = jsonEncode(queryParameters);
    request.add(utf8.encode(payload));
    HttpClientResponse response = await request.close();
    // print(response);
    if (response.statusCode == HttpStatus.ok) {
        String  _content = await response.transform(Utf8Decoder()).join();
        return_content; }}); }Copy the code

Config Configuration file

class CONFIG {
  static bool HTTP_MOCK = false;
  static String BaseHost = "192.168.0.111";
}
Copy the code

use

HTTP type

Future<String> HTTP(String path, Map<String, dynamic> queryParameters)
Copy the code

Using the example

String response_content = await HTTP('get_vxsaeoxaschterxsaxsano', {"id": id.toString()});
Copy the code

This is far from meeting production requirements, and we will improve it later


Once we have the requested data, we need to read and parse it. The next article is on JSON parsing and Model classes.

–END–