init static method
Future<void>
init({
- required String baseUrl,
- required dynamic userModelFromJson(),
- Function? onUnauthorizedCallback,
- AuthSettings? auth,
Initializes the Coffee
class.
Sets up the base URL for network requests, the JSON conversion function for model objects,
and an optional callback for handling 401 Unauthorized errors.
Additionally, initializes AuthSettings
which can include Microsoft authentication settings among others.
This method also initializes the GetStorage
library.
@param baseUrl - The base URL to be used throughout the app for network requests.
@param userModelFromJson - The function that converts a Map<String, dynamic>
to a model object.
@param onUnauthorizedCallback - (Optional) A callback function to execute when a 401 Unauthorized error occurs.
@param auth - (Optional) Authentication settings, including configurations for Microsoft authentication.
Example usage:
await Coffee.init(
baseUrl: 'https://api.example.com',
userModelFromJson: (map) => User.fromJson(map),
onUnauthorizedCallback: () {
// Define custom logic for handling 401 errors, e.g., show a dialog or redirect to login
},
auth: AuthSettings(
microsoftSettings: MicrosoftSettings(
clientId: "your-client-id",
redirectUri: "your-redirect-uri",
authority: "https://login.microsoftonline.com/your-tenant-id",
),
),
);
Implementation
static Future<void> init({
required String baseUrl,
required Function(Map<String, dynamic>) userModelFromJson,
Function? onUnauthorizedCallback,
AuthSettings? auth,
}) async {
_baseUrl = baseUrl;
_userModelfromJson = userModelFromJson;
_onUnauthorizedCallback = onUnauthorizedCallback;
_authSettings = auth;
await GetStorage.init();
}