flutterdart

part and export - What is the usage in dart?


I am a beginner in Dart and Flutter. I have seen lots of examples on GitHub using part and export keywords. I have searched on google, but still don't understand it clearly. I already read the answer When to use part/part of versus import/export in Dart?

still, I don't get it. Maybe it bit of a lengthy one and I am not a native English speaker.

  1. why are these keywords using?
  2. What they do?

Solution

  • Here is a simple code sample comprised of 3 files. Two files make up the library and one uses it. Explanations for all features are included in comments throughout the files.

    library_main.dart

    // UPDATE (2025): the `library` declaration is obsolete! it is
    // still available for legacy compatibility, but there is
    // no reason to use it anymore
    // declare a name for this library
    // NOTE: generally speaking, a single Dart file is a Library
    // `part` & `part of` allow us to divide the library among
    // multiple files
    library counter;
    
    // export adds contents of another Library to this Library's namespace
    // here we are adding all content (accessible from outside the library) from
    // the material library
    // NOTE: this is intended for related libraries
    // this arbitrary usage is simply for demonstration purposes
    export 'package:flutter/material.dart';
    
    // for finer control, we can use the 'show' directive
    // try this to see the effects of selected export
    // export 'package:flutter/material.dart' show StatefulWidget, State;
    
    // include any files containing parts of this library
    part 'library_part.dart';
    
    class Counter extends AbstractCounter {
      // we can access library private variable _count even though it is in a different
      // file because we made it part of the same library
      reset() => _count = 0;
    }
    

    library_part.dart

    // declare this file as part of the counter library
    part of counter;
    
    abstract class AbstractCounter {
      // this is a library private variable (_varName)
      // if this file were not made part of the counter library, this variable
      // would not be accessible to the Counter class in library_main.dart
      // this is an important concept to understand about Dart's library privacy
      // it differs from class based privacy, like that used in Java
      int _count = 0;
    
      get count => _count;
      increment() => ++_count;
    }
    

    library_example.dart

    // note that 'material.dart' does not need to be imported
    // because it has been exported with the counter library
    import 'library_main.dart';
    
    class LibraryExample extends StatefulWidget {
      @override
      _LibraryExampleState createState() => _LibraryExampleState();
    }
    
    class _LibraryExampleState extends State<LibraryExample> {
      final _counter = Counter();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Library Example'),
          ),
          body: Center(
            child: Text('You have pressed the button ${_counter.count} times.'),
          ),
          floatingActionButton: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              FloatingActionButton(
                child: Text('Reset'),
                onPressed: () => setState(() => _counter.reset()),
              ),
              SizedBox(width: 10),
              FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () => setState(() => _counter.increment()),
              ),
            ],
          ),
        );
      }
    }