Guide 3.3: Developer Guide
Using SdashAPI with Flutter
Flutter is Google's UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase. In this guide, we will use the popular http package to retrieve data from SdashAPI.
Setup and Dependencies
3.3.1First, add the http package to your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
http: ^1.1.0Fetching and Parsing with Dart
3.3.2Here is how you write a Future function in Dart to fetch a question, passing your API key via the headers map.
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<Map<String, dynamic>?> fetchQuestion() async {
final url = Uri.parse('https://sdashapi.com/api/v1/q?type=utme&subject=biology');
try {
final response = await http.get(
url,
headers: {
'Accept': 'application/json',
'AccessToken': 'YOUR_API_KEY_HERE',
},
);
if (response.statusCode == 200) {
final decodedData = json.decode(response.body);
if (decodedData['status'] == 200) {
return decodedData['data'];
}
}
return null;
} catch (e) {
print('Failed to load data: $e');
return null;
}
}