【问题标题】:part and export - What is the usage in dart?零件和出口 - 飞镖的用途是什么?
【发布时间】:2021-03-18 10:44:27
【问题描述】:

我是 Dart 和 Flutter 的初学者。我在 GitHub 上看到了很多使用 part 和 export 关键字的示例。我在谷歌上搜索过,但仍然不是很清楚。我已经看过答案When to use part/part of versus import/export in Dart?

不过,我还是不明白。也许它有点冗长,而且我不是以英语为母语的人。

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

【问题讨论】:

标签: flutter dart


【解决方案1】:

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

library_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;
}

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()),
          ),
        ],
      ),
    );
  }
}

【讨论】:

  • 所以你说一个简单的 dart 文件可以称为库,导出将导入库。那么部分呢?
  • 我已经更新了我的答案以更好地解决每个关键字。
  • 你能做一个简单的导出例子吗?那真的很有帮助
  • 没问题。查看更新的答案。我已经包含了一个简单的实现和大量的 cmets。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 2021-11-19
  • 2021-10-11
  • 2021-12-06
  • 1970-01-01
  • 2019-10-24
  • 2020-08-31
  • 1970-01-01
  • 2019-02-05
相关资源
最近更新 更多