如何在 flutter 中拆分飞镖 class?
how to split dart class in flutter?
我做了以下测试,但没有用:
//main.dart
class Test
{
static const a = 10;
final b = 20;
final c = a+1;
}
//part.dart
part of 'main.dart';
class Test
{
final d = a +1; //<---undefined name 'a'
}
我想将 flutter tutorial 中的 class 拆分成多个文件。例如:_buildSuggestions 在单独的文件中,_buildRow 在单独的文件中,等等。
更新:
我的解决方案:
之前:
//main.dart
class RandomWordsState extends State<RandomWords> {
{
final _var1;
final _var2;
@override
Widget build(BuildContext context) {
...
body: _buildList(),
);
Widget _buildList() { ... }
Widget _buildRow() { ... }
}
之后:
//main.dart
import 'buildlist.dart';
class RandomWordsState extends State<RandomWords> {
{
final var1;
final var2;
@override
Widget build(BuildContext context) {
...
body: buildList(this),
);
}
//buildlist.dart
import 'main.dart';
Widget buildList(RandomWordsState obj) {
... obj.var1 ...
}
Dart 不支持部分 classes。 part
和 part of
是将 库 拆分为多个文件,而不是 class.
Dart 中的私有(以 _
开头的标识符)是每个库,通常是一个 *.dart
文件。
main.dart
part 'part.dart';
class Test {
/// When someone tries to create an instance of this class
/// Create an instance of _Test instead
factory Test() = _Test;
/// private constructor that can only be accessed within the same library
Test._();
static const a = 10;
final b = 20;
final c = a+1;
}
part.dart
part of 'main.dart';
class _Test extends Test {
/// private constructor can only be called from within the same library
/// Call the private constructor of the super class
_Test() : super._();
/// static members of other classes need to be prefixed with
/// the class name, even when it is the super class
final d = Test.a +1; //<---undefined name 'a'
}
Dart 中的许多 code-generation 场景都使用了类似的模式,例如
- https://pub.dartlang.org/packages/built_value
- https://pub.dartlang.org/packages/built_redux
- https://pub.dartlang.org/packages/json_serializable
和许多其他人。
我遇到了同样的问题。我基于扩展的变体:
page.dart
part 'section.dart';
class _PageState extends State<Page> {
build(BuildContext context) {
// ...
_buildSection(context);
// ...
}
}
section.dart
part of 'page.dart';
extension Section on _PageState {
_buildSection(BuildContext context) {
// ...
}
}
我只是用Swift这样的扩展关键字来扩展它。
// class_a.dart
class ClassA {}
// class_a+feature_a.dart
import 'class_a.dart';
extension ClassA_FeatureA on ClassA {
String separatedFeatureA() {
// do your job here
}
}
请忽略编码约定,这只是一个示例。
我做了以下测试,但没有用:
//main.dart
class Test
{
static const a = 10;
final b = 20;
final c = a+1;
}
//part.dart
part of 'main.dart';
class Test
{
final d = a +1; //<---undefined name 'a'
}
我想将 flutter tutorial 中的 class 拆分成多个文件。例如:_buildSuggestions 在单独的文件中,_buildRow 在单独的文件中,等等。
更新:
我的解决方案:
之前:
//main.dart
class RandomWordsState extends State<RandomWords> {
{
final _var1;
final _var2;
@override
Widget build(BuildContext context) {
...
body: _buildList(),
);
Widget _buildList() { ... }
Widget _buildRow() { ... }
}
之后:
//main.dart
import 'buildlist.dart';
class RandomWordsState extends State<RandomWords> {
{
final var1;
final var2;
@override
Widget build(BuildContext context) {
...
body: buildList(this),
);
}
//buildlist.dart
import 'main.dart';
Widget buildList(RandomWordsState obj) {
... obj.var1 ...
}
Dart 不支持部分 classes。 part
和 part of
是将 库 拆分为多个文件,而不是 class.
Dart 中的私有(以 _
开头的标识符)是每个库,通常是一个 *.dart
文件。
main.dart
part 'part.dart';
class Test {
/// When someone tries to create an instance of this class
/// Create an instance of _Test instead
factory Test() = _Test;
/// private constructor that can only be accessed within the same library
Test._();
static const a = 10;
final b = 20;
final c = a+1;
}
part.dart
part of 'main.dart';
class _Test extends Test {
/// private constructor can only be called from within the same library
/// Call the private constructor of the super class
_Test() : super._();
/// static members of other classes need to be prefixed with
/// the class name, even when it is the super class
final d = Test.a +1; //<---undefined name 'a'
}
Dart 中的许多 code-generation 场景都使用了类似的模式,例如
- https://pub.dartlang.org/packages/built_value
- https://pub.dartlang.org/packages/built_redux
- https://pub.dartlang.org/packages/json_serializable
和许多其他人。
我遇到了同样的问题。我基于扩展的变体:
page.dart
part 'section.dart';
class _PageState extends State<Page> {
build(BuildContext context) {
// ...
_buildSection(context);
// ...
}
}
section.dart
part of 'page.dart';
extension Section on _PageState {
_buildSection(BuildContext context) {
// ...
}
}
我只是用Swift这样的扩展关键字来扩展它。
// class_a.dart
class ClassA {}
// class_a+feature_a.dart
import 'class_a.dart';
extension ClassA_FeatureA on ClassA {
String separatedFeatureA() {
// do your job here
}
}
请忽略编码约定,这只是一个示例。