From f29589d55b18f655b3e64bbc33f32f2fc19cc93f Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Thu, 17 Feb 2022 18:10:46 +0100 Subject: [PATCH] Add product service --- lib/service/product_service.dart | 24 ++++++++++++++++++++++++ pubspec.lock | 21 ++++++++++++++------- pubspec.yaml | 7 +------ test/product_service_test.dart | 10 ++++++++++ 4 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 lib/service/product_service.dart create mode 100644 test/product_service_test.dart diff --git a/lib/service/product_service.dart b/lib/service/product_service.dart new file mode 100644 index 0000000..dc71784 --- /dev/null +++ b/lib/service/product_service.dart @@ -0,0 +1,24 @@ +import 'dart:convert'; + +import 'package:http/http.dart'; +import 'package:thesis_shop/models/product.dart'; + +class ProductService { + final String url; + + ProductService({ + this.url = + "https://gist.githubusercontent.com/jonasfranz/3ba3b1d8453d2cb3a4571e20d7bd4eca/raw/747b131c48b894873711e040ad077df201180093/products.json", + }); + + Future> fetchProducts() async { + final result = await get(Uri.parse(url)); + final List parsed = jsonDecode(result.body); + return parsed + .map((e) => Product( + title: e['title'], + price: e['price'], + )) + .toList(); + } +} diff --git a/pubspec.lock b/pubspec.lock index 18b4a2b..e8671af 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -106,13 +106,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.17.1" - cupertino_icons: - dependency: "direct main" - description: - name: cupertino_icons - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.4" dart_code_metrics: dependency: "direct dev" description: @@ -172,6 +165,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.15.0" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.4" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" lints: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 999b482..3642c34 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,12 +29,7 @@ environment: 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: ^1.0.2 - + http: ^0.13.4 dev_dependencies: flutter_test: sdk: flutter diff --git a/test/product_service_test.dart b/test/product_service_test.dart new file mode 100644 index 0000000..2c31efa --- /dev/null +++ b/test/product_service_test.dart @@ -0,0 +1,10 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:thesis_shop/service/product_service.dart'; + +void main() { + test('test if service gets results from server', () async { + final products = await ProductService().fetchProducts(); + expect(products.length, greaterThan(0)); + expect(products.first.price, greaterThanOrEqualTo(0)); + }); +}