How to import only specific methods or classes in dart/flutter module

1,988

Background

Two days into my Flutter Journey, I faced a challenge with my android app's limitation on the number of methods that can be imported. This error is called 64K error. One of the suggestions the android team gives to avoid this error is to avoid importing unnecessary methods into the app.

So I needed to selectively import only the methods and classes I need and not the entire module. I couldn't find a way out so I came here on SO to find one but couldn't find any outright. So I made own question. Only after that did the automated system find a similar question which accurately answers the question. The Problem is that, wording of the question makes it not so easy to find. So I am making this post with a better title that hopefully will make it easy for others to find in the future.

Answer

So you want to import just one or two of the many methods/classes in the package or module?

use the usual import format and add show followed by a comma-separated list of the methods you want. Example:

import 'package:packageName/filename.dart' show class1, class2, method1;

But this method is useful if the you want a very small number of methods from the package.

Rather, if the number of methods you need is many you can't list all of them out. However, if you know which ones you don't need then you can excluded them using the same format but replace show with hide and list out those you don't need;

import 'package:packageName/filename.dart' hide class1, class2, method1;

I hope this answer helps someone;

Share:
1,988
OGB
Author by

OGB

Passionate coder

Updated on December 18, 2022

Comments

  • OGB
    OGB over 1 year

    I want to import only the classes/methods I am interested in, not the entire module. I am looking for the dart equivalent of the python statement from module import method/class or the equivalent of import module.method

  • daddygames
    daddygames over 2 years
    I was able to use this for export also. I have an enum, MyEnum, in it's own file with some other enums, "myenum.dart". I have a class, MyClass in "myclass.dart". MyClass uses MyEnum for the constructor and for methods which means that when I want to use MyClass, I would normally also need to include both "myclass.dart" and "myenum.dart" in the file where it's being utilized. However, by adding the following line to my "myclass.dart" file, I was able to expose MyEnum to any file that uses MyClass. export 'package:my_project/const/myenum.dart' show MyEnum;