anatomy post request flutter code example

Example 1: how to perform get request in flutter

import 'package:http/http.dart' as http;
import 'dart:convert';
class API
{
//replace with your endpoint
static String BASE_URL = 'https://some-url/api/';

 static Future<List<ExampleData>> getRequest() async {
   
    Response res = await http.get(BASE_URL+'example');
    
      if (res.statusCode == 200) {
      List<dynamic> body = jsonDecode(res.body);
        
     // complete by parsing the json body return into ExampleData object and return
     //.................
      }
}

}

Example 2: dart server listen how to recive json data

import 'dart:io';import 'dart:convert';String _host = InternetAddress.loopbackIPv4.host;Future main() async {  var server = await HttpServer.bind(_host, 4049);  await for (var req in server) {    ContentType contentType = req.headers.contentType;    HttpResponse response = req.response;    if (req.method == 'POST' &&        contentType?.mimeType == 'application/json' /*1*/) {      try {        String content =            await utf8.decoder.bind(req).join(); /*2*/        var data = jsonDecode(content) as Map; /*3*/        var fileName = req.uri.pathSegments.last; /*4*/        await File(fileName)            .writeAsString(content, mode: FileMode.write);        req.response          ..statusCode = HttpStatus.ok          ..write('Wrote data for ${data['name']}.');      } catch (e) {        response          ..statusCode = HttpStatus.internalServerError          ..write('Exception during file I/O: $e.');      }    } else {      response        ..statusCode = HttpStatus.methodNotAllowed        ..write('Unsupported request: ${req.method}.');    }    await response.close();  }}