Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created January 7, 2020 21:04
Show Gist options
  • Save azamsharp/f51439a20e73ee5673189055f3962fec to your computer and use it in GitHub Desktop.
Save azamsharp/f51439a20e73ee5673189055f3962fec to your computer and use it in GitHub Desktop.
import 'package:stocks_app_flutter/models/stock.dart';
import 'package:http/http.dart' as http;
class Webservice {
Future<List<Stock>> getStocks() async {
final url = "https://silicon-rhinoceros.glitch.me/stocks";
final response = await http.get(url);
if (response.statusCode == 200) {
Iterable json = jsonDecode(response.body);
return json.map((stock) => Stock.fromJson(stock)).toList();
} else {
throw Exception("Error fetching stocks");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment