kd_api_call 1.0.3 kd_api_call: ^1.0.3 copied to clipboard
A composable, multi-platform, Future-based API for HTTP requests.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:kd_api_call/kd_api_call.dart';
void main() {
// Run app...
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// Api response...
http.Response? apiResponse;
// Flag...
bool _isLoading = false;
@override
void initState() {
super.initState();
// Fetch data from internet...
fetchData();
}
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
// App bar...
appBar: AppBar(
title: const Text('Kd Api Call Example'),
),
// Body...
body: _buildBody(),
),
);
// Body...
Widget _buildBody() => Center(
child: _isLoading
? const CircularProgressIndicator.adaptive()
: apiResponse == null
? const Text('Something went wrong!')
: Text(apiResponse!.body),
);
// Fetch data from internet...
Future<void> fetchData() async {
try {
// Start loading...
if (mounted) {
setState(() {
_isLoading = true;
});
}
// Api request info...
APIRequestInfo requestInfo = APIRequestInfo(
url: "https://jsonplaceholder.typicode.com/albums/1",
requestType: HTTPRequestType.get,
);
// Call api...
// Await the http get response, then decode the json-formatted response.
apiResponse =
await ApiCall.instance.callService(requestInfo: requestInfo);
} on AppException catch (e) {
// Show error in snack bar...
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.getMessage),
),
);
} catch (e) {
// Show error in snack bar...
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(APIErrorMsg.somethingWentWrong),
),
);
} finally {
// Stop loading...
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
}