简体   繁体   中英

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?

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 eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM