This guide provides an overview of how to use the ApiRequest
class and the SuravLocal
class for making HTTP requests and managing local data storage in a Flutter application using the Dio package.
To use the ApiRequest
and SuravLocal
classes, you need to include the following dependencies in your pubspec.yaml
file:
dependencies:
surav: ^1.0.2+2
flutter:
sdk: flutter
Run flutter pub get
to install the dependencies.
To create an instance of the ApiRequest
class, you need to provide the URL and optional parameters such as method, body, options, and others.
ApiRequest request = ApiRequest(
'https://example.com/api/endpoint',
method: ApiMethod.POST,
body: {'key': 'value'},
showLogs: true, // Set to false to disable logging
);
To send a request, use the send
method. This method returns a Future<Response>
.
Response response = await request.send();
The send
method returns a Dio Response
object. You can handle the response as needed:
if (response.statusCode == 200) {
// Handle success
print(response.data);
} else {
// Handle error
print('Request failed with status: ${response.statusCode}');
}
Logging is handled by the Logger
package. The logging behavior depends on the request method and whether logging is enabled.
- GET requests: Informational logs
- POST and DELETE requests: Warning logs
- Other methods: Fatal logs
Logs include stack traces for better debugging.
The SuravLocal
class provides methods for managing local data storage using the shared_preferences
package.
To set a value in local storage, use one of the following methods:
await SuravLocal.setString('key', 'value');
await SuravLocal.setBool('key', true);
await SuravLocal.setInt('key', 123);
await SuravLocal.setDouble('key', 123.45);
await SuravLocal.setStringList('key', ['value1', 'value2']);
To retrieve a value from local storage, use one of the following methods:
String? stringValue = await SuravLocal.getString('key');
bool? boolValue = await SuravLocal.getBool('key');
int? intValue = await SuravLocal.getInt('key');
double? doubleValue = await SuravLocal.getDouble('key');
List<String>? stringListValue = await SuravLocal.getStringList('key');
To remove a specific value or clear all data from local storage, use the following methods:
await SuravLocal.remove('key');
await SuravLocal.clear();
The packFormData
function helps in packing data into a FormData
object for multipart/form-data requests.
FormData formData = packFormData({
'name': 'John Doe',
'file': await addFormFile('/path/to/file'),
});
The addFormFile
function helps in adding a file to the FormData
.
MultipartFile file = await addFormFile('/path/to/file', filename: 'file.txt');
Capitalizes the first letter of each word in a string.
String text = "hello world".capitalize();
// Output: "Hello World"
Extracts the file name from a file path.
String fileName = "/path/to/file.txt".getFileName();
// Output: "file.txt"
Truncates a file name to a maximum length of 22 characters, adding ".." if truncated.
String truncatedFileName = "/path/to/very_long_file_name.txt".getTruncatedFilenameWithDots();
// Output: "very_long_file_na..txt"
Extracts the file extension from a file path.
String fileExtension = "/path/to/file.txt".getFileExtension();
// Output: "txt"
Formats a date string in "YYYY-MM-DDTHH:MM:SS" format to "DD Mon YYYY".
String formattedDate = "2023-12-25T00:00:00".formatDate();
// Output: "25 Dec 2023"
Gets the last n
characters of a string.
String lastChars = "hello world".lastCharacters(5);
// Output: "world"
Gets the first n
characters of a string, followed by "...".
String firstChars = "hello world".firstCharacters(5);
// Output: "hello..."
Converts seconds to minutes, rounded to 3 decimal places.
double minutes = 120.toMinutes();
// Output: 2.000
Validates if a string is a valid email format.
bool isEmail = "example@example.com".isValidEmail();
// Output: true
Validates if a string is a valid phone number format.
bool isPhoneNumber = "+1-123-456-7890".isValidPhoneNumber();
// Output: true
Gets the width of the window.
double width = context.windowWidth;
Gets the height of the window.
double height = context.windowHeight;
Displays a toast message on the screen.
void showToast(
String msg, {
Toast toastLength = Toast.LENGTH_SHORT,
ToastGravity gravity = ToastGravity.BOTTOM,
int timeInSecForIosWeb = 1,
Color backgroundColor = Colors.black,
Color textColor = Colors.white,
double fontSize = 16.0,
}) {
Fluttertoast.cancel();
Fluttertoast.showToast(
msg: msg,
toastLength: toastLength,
gravity: gravity,
timeInSecForIosWeb: timeInSecForIosWeb,
backgroundColor: backgroundColor,
textColor: textColor,
fontSize: fontSize,
);
}
Here's a complete example demonstrating how to use
ElevatedButton(
onPressed: () => showToast('This is a toast message'),
child: Text('Show Toast'),
),
The SuravNavigate
class provides global keys for managing navigation and snackbars within a Flutter application. It simplifies the process of navigating between screens and displaying snackbars from anywhere in the application.
First, you need to initialize the SuravNavigate
class in your MaterialApp
widget. This ensures that the global keys are properly set up for managing navigation and snackbars.
import 'package:flutter/material.dart';
import 'surav_navigate.dart'; // Ensure this file includes the provided SuravNavigate class
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: SuravNavigate.navigateKey,
scaffoldMessengerKey: SuravNavigate.snackbarKey,
home: HomeScreen(),
);
}
}
To navigate to a new screen, you can use the navigate
global instance. This allows you to perform navigation operations from anywhere in your code.
NavigatorState navigate = SuravNavigate.navigateKey.currentState!;
void navigateToScreen(Widget screen) {
navigate.push(routeMe(screen));
}
To show a snackbar, you can use the snackbarKey
to get the current ScaffoldMessengerState
.
void showSnackbar(String message) {
SuravNavigate.snackbarKey.currentState!.showSnackBar(
SnackBar(content: Text(message)),
);
}
The routeMe
function is a utility that creates a MaterialPageRoute
for a given screen.
MaterialPageRoute routeMe(Widget screen) {
return MaterialPageRoute(builder: (context) {
return screen;
});
}
Here's a complete example demonstrating how to use the SuravNavigate
class and its related utilities in a Flutter application:
import 'package:flutter/material.dart';
import 'surav_navigate.dart'; // Ensure this file includes the provided SuravNavigate class
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: SuravNavigate.navigateKey,
scaffoldMessengerKey: SuravNavigate.snackbarKey,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home Screen')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
navigateToScreen(SecondScreen());
},
child: Text('Navigate to Second Screen'),
),
ElevatedButton(
onPressed: () {
showSnackbar('This is a snackbar message');
},
child: Text('Show Snackbar'),
),
],
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
navigate.pop();
},
child: Text('Go Back'),
),
),
);
}
}
void navigateToScreen(Widget screen) {
NavigatorState navigate = SuravNavigate.navigateKey.currentState!;
navigate.push(routeMe(screen));
}
void showSnackbar(String message) {
SuravNavigate.snackbarKey.currentState!.showSnackBar(
SnackBar(content: Text(message)),
);
}
Make sure to save the provided SuravNavigate
class in a file (e.g., surav_navigate.dart
) and import it in your main file as shown in the example. This will enable you to utilize the navigation and snackbar functionalities across your Flutter application.