5

In Dart, we have the ability to selectively import parts of files or libraries with the show keyword...


    import 'package:http/http.dart' show get;

...but Flutter makes tree-shaking. Has the show keyword any benefit in Flutter or is it completely meanless?

1 Answers1

12

import/exports directives have nothing to do with tree-shaking.

show/hide/as are instead used to not pollute the auto-complete, keep some classes "private", or resolve conflicts.

Say you're using both RxDart and Mobx: both packages define an Observable class.

If you tried to import both package:rxdart/rxdart.dart; and package:mobx/mobx.dart in the same file, then you would have a conflict.

You would, therefore, need to use show/hide/as to tell the compiler what's the solution.

It could be:

  • "I don't care about Mobx's Observable":
import 'package:rxdart/rxdart.dart';
import 'package:mobx/mobx.dart' hide Observable;
  • "I want only Mobx's "reaction":
import 'package:rxdart/rxdart.dart';
import 'package:mobx/mobx.dart' show reaction;
  • "I'll use an alias because I may use both"
import 'package:rxdart/rxdart.dart' as rxdart;
import 'package:mobx/mobx.dart' as mobx;
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • But let's say I have a simple issue, I need to know if my platform is IOS. I import `dart:io` to use `Platform.isIOS` but nothing else from the package. Will tree-shaking make the final app the same size as if I would have done `import 'dart:io' show Platform`? – Matthias S May 18 '20 at 19:47
  • 1
    Yes. As I said in the answer, tree-shaking works withot having to use `show` & co – Rémi Rousselet May 18 '20 at 21:25