parent
93846a57ea
commit
f29589d55b
@ -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(); |
||||||
|
} |
||||||
|
} |
@ -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…
Reference in new issue