1
0
Fork 0
Browse Source

Add product service

main
Jonas Franz 4 months ago
parent
commit
f29589d55b
  1. 24
      lib/service/product_service.dart
  2. 21
      pubspec.lock
  3. 7
      pubspec.yaml
  4. 10
      test/product_service_test.dart

24
lib/service/product_service.dart

@ -0,0 +1,24 @@ @@ -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<List<Product>> fetchProducts() async {
final result = await get(Uri.parse(url));
final List<dynamic> parsed = jsonDecode(result.body);
return parsed
.map((e) => Product(
title: e['title'],
price: e['price'],
))
.toList();
}
}

21
pubspec.lock

@ -106,13 +106,6 @@ packages: @@ -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: @@ -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:

7
pubspec.yaml

@ -29,12 +29,7 @@ environment: @@ -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

10
test/product_service_test.dart

@ -0,0 +1,10 @@ @@ -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));
});
}
Loading…
Cancel
Save