Skip to main content
The PortalFactory is a static service that manages portal registration and retrieval. It maintains a registry of all available portals and provides methods to access them by code or URL.

Properties

portals
List<Portal>
Returns a list of all registered portals.

Methods

registerPortal

Registers a single portal instance in the factory.
static void registerPortal(Portal portal)
portal
Portal
required
The portal instance to register. The portal will be indexed by both its URL and code.
Example:
final myPortal = MyPortal();
PortalFactory.registerPortal(myPortal);

registerAll

Registers multiple portals at once.
static void registerAll(List<Portal> portals)
portals
List<Portal>
required
A list of portal instances to register.
Example:
PortalFactory.registerAll([
  FlibustPortal(),
  LitresPortal(),
  BookRiverPortal(),
]);

fromCode

Retrieves a portal by its unique code identifier.
static Portal fromCode(String code)
code
String
required
The unique code identifier of the portal.
return
Portal
The portal instance matching the provided code.
Throws: ArgumentError if no portal is found with the given code. Example:
try {
  final portal = PortalFactory.fromCode('flibust');
  print('Found portal: ${portal.url}');
} catch (e) {
  print('Portal not found: $e');
}

fromUrl

Retrieves a portal by its URL origin.
static Portal fromUrl(Uri uri)
uri
Uri
required
The URI to match against registered portal URLs. Only the origin is used for matching.
return
Portal
The portal instance matching the provided URL origin.
Throws: ArgumentError if no portal is found with the given URL. Example:
try {
  final uri = Uri.parse('https://flibusta.site/book/12345');
  final portal = PortalFactory.fromUrl(uri);
  print('Found portal: ${portal.code}');
} catch (e) {
  print('Portal not found: $e');
}

fromJson

Deserializes a portal from JSON data.
static Portal fromJson(Map<String, dynamic> json)
json
Map<String, dynamic>
required
A JSON map containing a ‘code’ field that identifies the portal.
return
Portal
The portal instance identified by the code in the JSON data.
Example:
final json = {'code': 'flibust'};
final portal = PortalFactory.fromJson(json);

toJson

Serializes a portal to JSON data.
static Map<String, dynamic> toJson(Portal portal)
portal
Portal
required
The portal instance to serialize.
return
Map<String, dynamic>
A JSON map containing the portal’s code.
Example:
final portal = PortalFactory.fromCode('flibust');
final json = PortalFactory.toJson(portal);
// Returns: {'code': 'flibust'}

Usage

The PortalFactory maintains two internal registries:
  • By URL origin: Enables quick lookup when processing book URLs
  • By code: Enables lookup by portal identifier
Typical initialization:
void initializePortals() {
  PortalFactory.registerAll([
    FlibustPortal(),
    LitresPortal(),
    BookRiverPortal(),
  ]);
  
  print('Registered ${PortalFactory.portals.length} portals');
}

Build docs developers (and LLMs) love