Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional import for server/console only #59786

Open
jodinathan opened this issue Dec 21, 2024 · 2 comments
Open

Conditional import for server/console only #59786

jodinathan opened this issue Dec 21, 2024 · 2 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality

Comments

@jodinathan
Copy link

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?

@dart-github-bot
Copy link
Collaborator

Summary: User wants conditional imports to implement platform-specific data fetching (network vs. database) while maintaining a consistent function signature across client and server code. They're seeking guidance on achieving this.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality labels Dec 21, 2024
@jodinathan
Copy link
Author

just found out how to do it:

export 'stub.dart' 
if (dart.library.html) 'a.dart' 
if (dart.library.ui) 'a.dart' 
if (dart.library.io) 'b.dart';

dart:ui isn't available in console apps, so this works :)

I will leave this open for couple of days if anyone want to comment about the reliability of this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

2 participants