Quabs
Quabs

Reputation: 51

Flutter: import package only for web users

I see that Flutter has conditional import statements, but after looking at some examples I’m still confused.

If I want to have main.dart import package “package:xyz/xyz.dart” only when the user is on web, how can I achieve that in the simplest way? Thanks for any tips.

Upvotes: 0

Views: 1839

Answers (1)

Stefano Amorelli
Stefano Amorelli

Reputation: 4854

We could import a package only for the web users by the use of a conditional import:

import 'package:xyz/mobile.dart' 
    if (dart.library.html) 'package:xyz/web.dart';

The code above imports package:xyz/web.dart only if dart.library.html is available, which happens to be the case for the web platform.

Upvotes: 5

Related Questions