Last active
July 11, 2020 15:31
-
-
Save TetsuFe/b86752caed0e7d59bf2dbe194b119542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:io'; | |
class Item { | |
Item({this.id, this.price, this.name}); | |
Item.fromJson(Map<String, dynamic> json) | |
: this.id = json["id"], | |
this.price = json["price"], | |
this.name = json["name"]; | |
final int id; | |
final int price; | |
final String name; | |
} | |
class Cart { | |
Cart({this.items}); | |
final List<Item> items; | |
int get totalPrice => items.fold(0, (cur, item) => cur + item.price); | |
} | |
class CartApiClient { | |
Future<Map<String, dynamic>> fetchMyCart() async { | |
final parsedUri = | |
Uri.parse('https://run.mocky.io/v3/d1634fbf-3860-479e-b049-4dcba1d503d2'); | |
final myCartRequest = await HttpClient().getUrl(parsedUri); | |
final myCartResponse = await myCartRequest.close(); | |
final myCartJsonString = await utf8.decodeStream(myCartResponse); | |
final myCart = jsonDecode(myCartJsonString); | |
return myCart; | |
} | |
} | |
class MockCartApiClient { | |
Future<Cart> fetchMyCart() async { | |
return Future.value(Cart(items: [ | |
Item(id: 1, price: 100, name: 'apple'), | |
Item(id: 2, price: 200, name: 'banana') | |
])); | |
} | |
} | |
void main() async { | |
final myCart = await MockCartApiClient().fetchMyCart(); | |
print(myCart.totalPrice); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment