Skip to content

Usage with muli-platform apps #2

Answered by Zekfad
Deishelon asked this question in Q&A
Discussion options

You must be logged in to vote

You can use http module on all platforms. Use conditional imports to import target client, e.g. like this:

// platfrom.dart
export 'platform/stub.dart'
  if (dart.library.io) 'platform/io.dart'
  if (dart.library.js) 'platform/js.dart'
  if (dart.library.html) 'platform/js.dart';
// platform/stub.dart
/// Returns platform appropriate [Client].
Client getClient() {
  throw UnsupportedError('Stub code is called');
}
// platform/js.dart
/// Returns platform appropriate [Client].
Client getClient() => FetchClient(/* ... */);
// platform/io.dart
/// Returns platform appropriate [Client].
Client getClient() => IOClient(/* ... */);

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Zekfad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #1 on February 02, 2023 11:22.