我做了以下测试,但不起作用:

//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个文件.例如:_buildSuggestions在单独的文件中,_buildRow在单独的文件中,等等.

update:

我的解决方案:

之前:

//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不支持分部类.partpart of将把library分成多个文件,而不是一个类.

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中的许多代码生成场景中使用,如中

还有其他许多人.

Flutter相关问答推荐

为什么当我升级时我的项目没有运行

Flutter:如何在Android上的其他应用程序中恢复音频播放

我遇到了一个问题,从showDialogue()调用Navigator.pop()两次导致屏幕空白

Flutter FireRestore:如何从特定/实际领域获取数据

使用Flutter 提供程序包加载状态应用程序时,在自定义按钮中显示CircularProgressIndicator

为什么Flutter InAppWebView中的DatePicker看起来很旧?

如何将Will POP示波器转换为POP数据抖动的POP示波器

我不能在Fighting中裁剪图片输入错误,否则应用程序会崩溃

如何管理枚举类型的阻塞事件?

有没有可能不在BlocBuilder中重新构建内容?

在flutter web上渲染多个Youtube视频

摆动更改标记可轻敲文本按钮样式

抖动问题:检测到0个或2个或更多具有相同值的[DropdownMenuItem]

处理 flutter/dart 模型中的空值

Flutter 中的区域不匹配

当键盘隐藏表单时该怎么办?

从子类调用超类的超方法

如何修复 ICU Lexing 错误:Flutter 中的意外字符

使用 Firebase/Flutter 进行应用判断 - 100% 无效请求

小部件库捕获的 Flutter 异常