Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Umbrella Imports with Dart/Flutter

I am developing a plugin for Dart (Flutter). I have split up the source into many different implementation files to keep things clean and avoid having one massive file.

The problem is, I don't want users to have to import tons of source files whenever they want to use my package.

Is there any way, in flutter or Dart itself, to be able to declare some sort of umbrella interface?

like image 904
Brad Hesse Avatar asked Oct 19 '25 14:10

Brad Hesse


1 Answers

In your plugin, you have a lib folder. Create a lib/src sub-folder and move the bulk of your implementation files there. It's typical to be left with just one file in lib e.g. someplugin.dart.

In there you can have any top level classes or functions but this is where you include the implementation source files using the export directive.

Here's an example from the google_sign_in plugin, from google_sign_in.dart:

import 'dart:async';
import 'dart:ui' show hashValues;

import 'package:flutter/services.dart' show MethodChannel;
import 'package:meta/meta.dart' show visibleForTesting;

import 'src/common.dart'; // this import is only required if used by some top level
                          // class lower down this file

export 'src/common.dart'; // this export means that your plugin's users don't need
                          // to import it themselves
like image 195
Richard Heap Avatar answered Oct 22 '25 02:10

Richard Heap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!