Skip to content

Conditional import for server/console only #59786

Closed
@jodinathan

Description

@jodinathan

I want to separate client and server logic into different files using conditional imports.

For clients, such as web and mobile, the implementation should use network calls to fetch data from the API (which serves as the backend).
On the other hand, the server should connect directly to the database without relying on an HTTP call.

The goal is to have the same function signature across all platforms:

extension SomeExtension on SomeClass {
  Future<List<User>> getUsers() {}
}

On mobile or web (or Flutter in general), getUsers should make an HTTP request to the API.
When running in a console application (server-side), getUsers should interact directly with the database.

I've experimented with various dart.library.something configurations but haven’t been able to achieve this functionality.

Is this approach feasible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.triage-automationSee https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot.type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions