How to import only specific methods or classes in dart/flutter module
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;
Comments
-
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 ofimport module.method
-
daddygames over 2 yearsI 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
usesMyEnum
for the constructor and for methods which means that when I want to useMyClass
, 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 exposeMyEnum
to any file that usesMyClass
.export 'package:my_project/const/myenum.dart' show MyEnum;