部分和导出 - 在 dart 中有何用途?

beg*_*ner 11 dart flutter

我是 Dart 和 Flutter 的初学者。我在 GitHub 上看到了很多使用part和export关键字的例子。我在google上搜索过,但还是不太明白。我已经阅读了答案When to use part/part of vs import/export in Dart?

我还是不明白。也许这有点长,而且我的母语不是英语。

  1. 为什么使用这些关键词?
  2. 他们做什么?

Lee*_*ee3 26

这是一个由 3 个文件组成的简单代码示例。该库由两个文件组成,一个文件使用它。所有功能的说明都包含在整个文件的注释中。

库_main.dart

// declare a name for this library to reference from parts
// this is not necessary if we do not need to reference elsewhere
// NOTE: generally, a Dart file is a Library
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;
}
Run Code Online (Sandbox Code Playgroud)

库部分.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;
}
Run Code Online (Sandbox Code Playgroud)

库示例.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()),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 没问题。查看更新的答案。我已经包含了一个简单的实现和大量的评论。希望能帮助到你。 (3认同)