0 purchases
utility flutter
utility_flutter #
This package provides a comprehensive utility to handle common HTTP and shared preferences operations with ease. Built around the http and shared_preferences package, it integrates seamlessly with Flutter and Dart applications, offering features like setting default headers, configuring request timeouts, and delivering standardized error responses and storing smale variables in device.
🌟 Features #
HttpUtils #
Simplified HTTP Requests: Perform GET, POST, PUT, and DELETE operations with just a few lines of code.
Consistent Base URL: Define a base URL once and forget about repeating it for every request.
Default Headers: Easily set default headers that automatically accompany every HTTP request.
Timeouts: A default timeout duration ensures your requests don't hang indefinitely.
Standardized Errors: Understand and handle HTTP errors better with standardized error responses.
Create an instance of HttpUtils: Create an instance of the HttpUtils class by providing the baseUrl for your API. You can also specify optional parameters like defaultHeaders and timeoutDuration. For example:
final httpUtils = HttpUtils('https://api.example.com', defaultHeaders: {
'Authorization': 'Bearer your_token_here',
});
copied to clipboard
GET Request:
try {
final response = await httpUtils.get('/endpoint');
// Handle the response
} catch (e) {
// Handle errors
}
copied to clipboard
POST Request:
try {
final body = {'key': 'value'};
final response = await httpUtils.post('/endpoint', body: body);
// Handle the response
} catch (e) {
// Handle errors
}
copied to clipboard
PUT Request:
try {
final body = {'key': 'value'};
final response = await httpUtils.put('/endpoint', body: body);
// Handle the response
} catch (e) {
// Handle errors
}
copied to clipboard
DELETE Request:
try {
final response = await httpUtils.delete('/endpoint');
// Handle the response
} catch (e) {
// Handle errors
}
copied to clipboard
if your API have no need of defaultHeaders or headers, you no need to initializes this .
PreferencesUtils #
Initialize the PreferencesUtils instance : use in your project .
void main() async {
WidgetsFlutterBinding.ensureInitialized(); // Required to use async methods in the main function.
final preferences = PreferencesUtils();
await preferences.init();
runApp(MyApp());
}
copied to clipboard
Initialize the PreferencesUtils instance
// Get a PreferencesUtils instance.
final preferences = PreferencesUtils();
// Storing data:
await preferences.setBool('isDarkMode', true);
await preferences.setInt('userAge', 25);
await preferences.setDouble('userHeight', 175.5);
await preferences.setString('username', 'navneet');
copied to clipboard
isDarkMode, userAge, userHeight, username these are key using this you can get stored data from your device.
// Retrieving data:
bool isDarkMode = preferences.getBool('isDarkMode');
int userAge = preferences.getInt('userAge');
double userHeight = preferences.getDouble('userHeight');
String username = preferences.getString('username');
// Clearing preferences:
preferences.clear();
copied to clipboard
🚀 Getting Started #
Prerequisites #
To use this package:
Ensure you have Flutter and Dart SDK installed.
Installation #
Add the package to your pubspec.yaml:
dependencies:
utility_package: <latest_version>
copied to clipboard
or Run this command:
flutter pub add utility_flutter
copied to clipboard
Import it in your Dart code, you can use:
import 'package:utility_flutter/utility_flutter.dart';
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.