相关疑难解决方法(0)

dart中名为._()函数的类方法?

香港专业教育学院看到这段代码,任何人都可以向我解释一下它的AppTheme._()含义,因为我在dart中了解了它的单例类,但我真的不明白它的工作原理。

class AppTheme {
  AppTheme._();

  static const Color notWhite = Color(0xFFEDF0F2);
  static const Color nearlyWhite = Color(0xFFFEFEFE);
  static const Color white = Color(0xFFFFFFFF);
  static const Color nearlyBlack = Color(0xFF213333);

.
.
.
.
}
Run Code Online (Sandbox Code Playgroud)

dart flutter

5
推荐指数
2
解决办法
88
查看次数

如何使用空安全构建空工厂构造函数?

用于创建静态方法持有者类的空安全之前的常见 Dart 模式如下:

class MyMethodScope {
  /// Prevents instantiation of this class.
  factory MyMethodScope._() => null;

  static void noop() {}
}
Run Code Online (Sandbox Code Playgroud)

这对于空安全是不可能的,因为工厂构造函数的返回类型显然不可为空。

dart flutter dart-null-safety

5
推荐指数
1
解决办法
2455
查看次数

如何在命名构造函数中使用异步?

我创建了一个类,我想在命名构造函数或类中可在类外访问的方法中使用异步。当进行命名的建筑工返回一个Future类型,我得到一个错误说:Constructors can't have a return type

然后我尝试删除Future类型,但我仍然收到一条错误消息,说The modifier 'async' can't be applied to the body of a constructor.

如何在命名构造函数中使用异步?

class HttpService {
  Future<void> HttpService.getAll() async {
    final response = await http.get(
      Uri.encodeFull('http://localhost:4000/'),
      headers: {'headers': 'application/json'},
    );

    if (response.statusCode == 200) {}
  }
}
Run Code Online (Sandbox Code Playgroud)

我是 oop 的新手,所以我可能用错了它?接受任何指导。

dart flutter

2
推荐指数
2
解决办法
3210
查看次数

标签 统计

dart ×3

flutter ×3

dart-null-safety ×1