Flutter: Is There a Performance Penalty for a Single Global Widget Import?

Viewed 40

I am new to Flutter and have created a bunch of UI elements as custom widgets so that I can use them throughout my app. It's tiresome to have to remember to include each and every widget import in a Dart file.

I recently learned that you can export a widget from within another widget, and anything that imports the parent widget has access to everything:

//===================
//=== global.dart ===
//===================

import 'package:flutter/material.dart';
import 'package:myapp/storage.dart';

//Widget exports for convenience
export 'package:myapp/_global/widgets/widget1.dart';
export 'package:myapp/_global/widgets/widget2.dart';
export 'package:myapp/_global/widgets/widget3.dart';

So now in each of my files throughout my project, I just need to do:

import 'package:myapp/global.dart';

...and I have access to widget1, widget2, widget3.

Is there a risk to doing this, such as a performance decrease or something else I can't foresee? It's awfully convenient for development. I'm just trying to make sure I'm not shooting myself in the foot.

1 Answers

This type of file exporting is called "barrel" exporting. There's an extension for both Visual Studio Code Extension and Android Studio extension that does this automatically for you!

Is there a risk to doing this, such as a performance decrease or something else I can't foresee?

I don't think there's a risk, though I don't know. I have taken a Flutter course from an expert who recommends doing barrel import. And actually, if you look at the source code for some Flutter packages, you'll see that that's exactly what they do.

For example, take a look at the most used import of import 'package:flutter/material.dart';, you'll see that they use barrel importing:

...
library material;

export 'src/material/about.dart';
export 'src/material/animated_icons.dart';
export 'src/material/app.dart';
export 'src/material/app_bar.dart';
export 'src/material/app_bar_theme.dart';
export 'src/material/arc.dart';
export 'src/material/autocomplete.dart';
export 'src/material/back_button.dart';
Related