Last active
August 3, 2019 16:23
-
-
Save Eng-MFQ/151438400728fe94be12fcdbaf478d5d 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:async'; | |
import 'dart:convert'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
/// to see the response go to | |
/// https://gist.github.com/Eng-MFQ/483ac384385134dc3d5d30d236419b6c | |
final String dessertResp = '[ { "product_id" : 1, "name" : "Chocolate Cake", "imgUrl" : "https://github.com/Eng-MFQ/imges_uplader/blob/master/Dessert/chcolate%20cake.jpg?raw=true", "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "type" : "dessert", "quantity" : 13, "Price" : "1.80 JD" }, { "product_id" : 2, "name" : "Toffee Cake", "imgUrl" : "https://github.com/Eng-MFQ/imges_uplader/blob/master/Dessert/niscafi%20cake.jpg?raw=true", "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "type" : "dessert", "quantity" : 15, "Price" : "2.80 JD" }, { "product_id" : 3, "name" : "Layers Cake", "imgUrl" : "https://github.com/Eng-MFQ/imges_uplader/blob/master/Dessert/layers%20cake.jpg?raw=true", "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "type" : "dessert", "quantity" : 10, "Price" : "1.40 JD" }, { "product_id" : 4, "name" : "Cheese Cake", "imgUrl" : "https://github.com/Eng-MFQ/imges_uplader/blob/master/Dessert/cheese%20cake.jpg?raw=true", "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "type" : "dessert", "quantity" : 30, "Price" : "1.00 JD" }, { "product_id" : 5, "name" : "Red Cake", "imgUrl" : "https://github.com/Eng-MFQ/imges_uplader/blob/master/Dessert/red%20cake.jpg?raw=true", "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "type" : "dessert", "quantity" : 32, "Price" : "1.30 JD" }, { "product_id" : 6, "name" : "Despacito", "imgUrl" : "https://github.com/Eng-MFQ/imges_uplader/blob/master/Dessert/decpacito.jpg?raw=true", "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "type" : "dessert", "quantity" : 16, "Price" : "1.50 JD" } ]'; | |
main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: DessertList(), | |
); | |
} | |
} | |
class DessertList extends StatefulWidget { | |
@override | |
_DessertListState createState() => _DessertListState(); | |
} | |
class _DessertListState extends State<DessertList> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
/// implement the Future builder by yourself | |
/// implement the design by yourself | |
body: Text('$dessertResp'), | |
); | |
} | |
} | |
Future<List<Dessert>> fetchDesserts() async { | |
// fake call to API | |
final response = await http.get('https://jsonplaceholder.typicode.com/photos'); | |
// move parsing to another thread | |
return compute(parseDesserts, dessertResp); | |
} | |
// A function that converts a response body into a List<Dessert>. | |
List<Dessert> parseDesserts(String dessertResp) { | |
final parsed = json.decode(dessertResp).cast<Map<String, dynamic>>(); | |
return parsed.map<Dessert>((json) => Dessert.fromJson(json)).toList(); | |
} | |
// dessert class | |
class Dessert { | |
/// Implement the variables by yourself | |
Dessert(); | |
factory Dessert.fromJson(Map<String, dynamic> json) { | |
/// implement JSON parsing by yourself | |
return Dessert(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment