الأدلة
أسعار الذهب في تطبيق Flutter
اقرأ أسعار الذهب لكل عيار بلغة Dart، واحفظها بضع دقائق، واعرض وقت تحديثها.
الطلب GET /v1/gold يرد بسعر الجرام لكل عيار بالجنيه المصري، وسعر الجنيه الذهب، وسعر الأوقية بالدولار. وهو متاح في الخطة المجانية.
اقرأ الأسعار
Dart
import 'dart:convert';
import 'package:http/http.dart' as http;
class GoldPrice {
final String karat;
final double buy, sell;
GoldPrice(this.karat, this.buy, this.sell);
}
DateTime? _at;
List<GoldPrice> _cache = [];
Future<List<GoldPrice>> goldPrices(String key) async {
// Prices change a few times an hour, so keep them 5 minutes.
if (_at != null && DateTime.now().difference(_at!).inMinutes < 5) return _cache;
final res = await http.get(Uri.parse('https://api.geneh.app/v1/gold'),
headers: {'Authorization': 'Bearer $key'});
final data = jsonDecode(res.body) as Map<String, dynamic>;
_cache = [
for (final p in data['prices'])
if ((p['instrument'] as String).endsWith('k_g'))
GoldPrice(p['instrument'], (p['buy'] as num).toDouble(), (p['sell'] as num).toDouble()),
];
_at = DateTime.now();
return _cache;
}احمِ مفتاحك
يستطيع أي شخص قراءة المفتاح إذا وضعته داخل التطبيق. لذلك في التطبيق المنشور اطلب الواجهة البرمجية من خادمك واحفظ الرد هناك. هكذا يخدم مفتاح واحد كل المستخدمين ويبقى داخل حدود خطتك.
اعرض updated_at بجوار الأسعار دائمًا، ليعرف الناس متى حُدّثت.
أسعار الذهب