From 4263997c3e419ef1cf7447d447c5582a322acf74 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 20 Jul 2018 18:14:18 +0200 Subject: Began basic login screen feature (mvp architecture) --- lib/utils/network_util.dart | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/utils/network_util.dart (limited to 'lib/utils') diff --git a/lib/utils/network_util.dart b/lib/utils/network_util.dart new file mode 100644 index 0000000..463e98f --- /dev/null +++ b/lib/utils/network_util.dart @@ -0,0 +1,38 @@ +import 'dart:async'; +import 'dart:convert'; +import 'package:http/http.dart' as http; + +class NetworkUtil { + // next three lines makes this class a Singleton + static NetworkUtil _instance = new NetworkUtil.internal(); + NetworkUtil.internal(); + factory NetworkUtil() => _instance; + + final JsonDecoder _decoder = new JsonDecoder(); + + Future get(String url) { + return http.get(url).then((http.Response response) { + final String res = response.body; + final int statusCode = response.statusCode; + + if (statusCode < 200 || statusCode > 400 || json == null) { + throw new Exception("Error while fetching data"); + } + return _decoder.convert(res); + }); + } + + Future post(String url, {Map headers, body, encoding}) { + return http + .post(url, body: body, headers: headers, encoding: encoding) + .then((http.Response response) { + final String res = response.body; + final int statusCode = response.statusCode; + + if (statusCode < 200 || statusCode > 400 || json == null) { + throw new Exception("Error while fetching data"); + } + return _decoder.convert(res); + }); + } +} \ No newline at end of file -- cgit v1.2.3