Posts

Creating powerful apps with smart design, innovative UI/UX, and solid principles.

Flutter Dio Logger: Simplifying HTTP Request & Response Logging | Custom Log

Image
Explore a comprehensive log utility class for Flutter using the Dio HTTP client.  Learn how to efficiently log HTTP requests, responses, and errors with different log levels and color-coded messages, making debugging and error handling a breeze in your Flutter app development util / logger_interceptor.dart import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; // Author: Hari Shankar // Date: 28-07-2023 /* No 3rd party plugin Purpose: Flutter Dio Logger: Simplifying HTTP Request & Response Logging. */ // Define an enum for the different log levels enum Level { debug, info, warning, error, alien } // Define a logDebug function that logs messages at the specified level // log different colors void logDebug(String message, {Level level = Level.info}) { // Define ANSI escape codes for different colors const String resetColor = '\x1B[0m'; const String redColor = '\x1B[31m'; // Red const String green...

Dio MVVM Get API Integration with Provider in Flutter (With Nested JSON Object)

Image
Learn how to integrate Dio (HTTP client) with MVVM architecture to perform Get API calls efficiently (With Nested JSON Object). Explore sample code, step-by-step tutorial, and best practices for seamless API integration. dependencies: flutter: sdk: flutter dio: ^5.3.0 provider: ^6.0.5 Dio MVVM Product List App Build a Flutter product list app using Dio for API calls and MVVM architecture. Learn how to manage user data and API integration efficiently with sample code and best practices. main.dart  : The main entry point of the Flutter app. import 'package:flutter/material.dart'; import 'package:dio/dio.dart'; import 'package:flutteryfly/viewmodels/product_view_model.dart'; import 'package:flutteryfly/views/product_list.dart'; import 'package:provider/provider.dart'; import './data/repositories/user_repository.dart'; import './data/services/api_service.dart'; import 'data/repositories/product_repository.dart'; void...

Dio MVVM Get API Integration with GetX in Flutter

Image
Learn how to integrate Dio (HTTP client) with MVVM architecture to perform Get API calls efficiently. Explore sample code, step-by-step tutorial, and best practices for seamless API integration. dependencies: flutter: sdk: flutter dio: ^5.3.0 get: ^4.6.5 MVVM folder structure lib/ ├── data/ │ ├── models/ │ │ └── user.dart // Contains the User model class for representing user data. │ ├── repositories/ │ │ └── user_repository.dart // Handles fetching user data from the API. │ └── services/ │ └── api_service.dart // Provides methods to interact with the API using Dio or other HTTP clients. │ ├── utils/ │ ├── constants.dart // Contains constant values used throughout the app. │ └── logger.dart // Utility for logging app events or errors. │ ├── view/ │ └── user_list.dart // View file for displaying the list of users. │ ├── view_models/ │ └── user_view_model.dart // ViewModel class for managing user data and API calls. │ ├── widgets/ │ ├── user_tile.d...

Dio MVVM Get API Integration with Provider in Flutter

Image
Learn how to integrate Dio (HTTP client) with MVVM architecture to perform Get API calls efficiently. Explore sample code, step-by-step tutorial, and best practices for seamless API integration. dependencies: flutter: sdk: flutter dio: ^5.3.0 provider: ^6.0.5 MVVM folder structure lib/ ├── data/ │ ├── models/ │ │ └── user.dart // Contains the User model class for representing user data. │ ├── repositories/ │ │ └── user_repository.dart // Handles fetching user data from the API. │ └── services/ │ └── api_service.dart // Provides methods to interact with the API using Dio or other HTTP clients. │ ├── utils/ │ ├── constants.dart // Contains constant values used throughout the app. │ └── logger.dart // Utility for logging app events or errors. │ ├── view/ │ └── user_list.dart // View file for displaying the list of users. │ ├── view_models/ │ └── user_view_model.dart // ViewModel class for managing user data and API calls. │ ├── widgets/ │ ├── user_t...

Flutter Shared Preference - Manage Local Storage Data in Flutter Apps

Managing Local Storage Data in Flutter Apps with Shared Preferences and a Common Utility Class It provides a simple key-value storage solution that can be used to store and retrieve primitive data types, such as booleans, integers, doubles, strings, and lists of strings.  This makes it a useful tool for managing user preferences, caching data, and persisting app state. dependencies: flutter: sdk: flutter shared_preferences: ^2.2.0

Flutter Navigation Common Util Class

Image
A utility class for common navigation functions in Flutter applications, including navigating to new routes, replacing routes, removing previous routes, and handling navigation errors. util / navigation_utils.dart import 'package:flutter/material.dart'; class NavigationUtil { /// Navigates to a new route. /// /// The `routeName` parameter specifies the name of the route to navigate to. /// The `arguments` parameter (optional) provides additional arguments to pass to the new route. static void navigateTo(BuildContext context, String routeName, {Object? arguments}) { try { Navigator.pushNamed(context, routeName, arguments: arguments); } catch (error) { debugPrint('Navigation Error: $error'); // Handle the error as needed } } /// Navigates to a new route asynchronously and returns a result from the destination screen. /// /// The `routeName` parameter specifies the name of the route to navigate to. /// The `arguments` param...

Mastering Null-aware Operators in Dart: Safely Handling Null Values

Improve code safety and readability by efficiently dealing with null values in your Dart applications. Learn how to effectively handle null values in Dart using null-aware operators, including  null-aware access ( ?. ),  null-aware assignment ( ??= ),  null-aware cascade ( .. ), and  null-aware conditional ( ?? ).  .. Null-aware access operator (?.):  It is used to access properties or call methods on an object that may be null. If the object is null, the expression returns null without throwing an exception. class Person { String name; Person(this.name); void sayHello() { print('Hello, $name!'); } } void main() { Person? person; // Nullable Person object person?.sayHello(); // This will not throw an exception because of the null-aware access operator } .. Null-aware assignment operator (??=):   This operator assigns a value to a variable only if the variable is currently null. It is a handy way to set default values for nullable vari...