如何定义返回同一类的对象的函数?
我正在做这个示例教程在异步函数上并查看本节,
import 'dart:io';
class ProceessedData {
ProceessedData(this.data);
final String data;
Future<String> empty() {
return Future.delayed(const Duration(seconds: 4),
() => HttpException('http://www.google.com.').toString());
}
}
void main(List<String> args) {
Future<String> _loadFromDisk() {
return Future.delayed(const Duration(seconds: 4), () => 'Lectura de disco');
}
Future<String> _fetchNetworkData(String codigo) {
return Future.delayed(
const Duration(seconds: 4), () => 'lectura de red codigo $codigo');
}
Future<ProceessedData> createData() async {
try {
final id = await _loadFromDisk();
final data = await _fetchNetworkData('4');
return ProceessedData(data);
} on HttpException catch (err) {
print('Network error: $err');
return ProceessedData.empty();
} finally {
print('All done!');
}
}
}
我看到在例外proceesseddata.empty()
我必须在我的主类中定义,因为它不存在,但我认为它是这样。应按照以下方式定义
Future<String> empty() {
return Future.delayed(const Duration(seconds: 4),
() => HttpException('http://www.google.com.').toString());
}
,但是在验证时,此错误仍表示
如果我很清楚,我正在返回类型字符串的未来值,以及我拥有的功能创建createata()
指示它将返回processeddata
,但我不知道如何定义empty()
函数,以便它返回同一班级的一个实例(我觉得如果您想循环)
I am doing this example tutorial on async functions and reviewing this section
import 'dart:io';
class ProceessedData {
ProceessedData(this.data);
final String data;
Future<String> empty() {
return Future.delayed(const Duration(seconds: 4),
() => HttpException('http://www.google.com.').toString());
}
}
void main(List<String> args) {
Future<String> _loadFromDisk() {
return Future.delayed(const Duration(seconds: 4), () => 'Lectura de disco');
}
Future<String> _fetchNetworkData(String codigo) {
return Future.delayed(
const Duration(seconds: 4), () => 'lectura de red codigo $codigo');
}
Future<ProceessedData> createData() async {
try {
final id = await _loadFromDisk();
final data = await _fetchNetworkData('4');
return ProceessedData(data);
} on HttpException catch (err) {
print('Network error: $err');
return ProceessedData.empty();
} finally {
print('All done!');
}
}
}
I see that in the exception ProceessedData.empty()
which I must define in my main class since it does not exist but I thought it should be defined in the following way
Future<String> empty() {
return Future.delayed(const Duration(seconds: 4),
() => HttpException('http://www.google.com.').toString());
}
but when validating, this error is still indicated
If it is clear to me, I am returning a future value of type string and that the function that I have created createData()
indicates that it will return a ProcessedData
but I do not know how I should define the empty()
function so that it returns an instance of its same class (I feel like if you were looking to loop)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
processeddata.empty()
的目的是创建一个没有数据的新实例processeddata
(不是String
)。直接在processeddata
类上直接调用它的事实,意味着empty()
是静态方法,工厂构造函数或命名的构造函数(这些中的任何一个都将做)。它还不需要返回
Future
- 无需等待任何内容即可获取空数据的实例。createAta
是一种异步方法,这意味着 值从该方法中返回processeddata
。考虑到所有这些,您可以定义
empty
这样:The purpose of
ProcessedData.empty()
is to create a new instance ofProcessedData
(notString
) which has no data. The fact that it is called directly on theProcessedData
class, means thatempty()
is either a static method, a factory constructor, or a named constructor (any of those will do).It also doesn't need to return a
Future
- There is no need to wait for anything to get an instance of empty data. The fact thatcreateData
is an async method means that any value returned from the method will already be wrapped in aFuture
, so you can just return aProcessedData
.With all that in mind, you can define
empty
like this:我不明白您为什么要添加未来
。
I don't understand why you're adding Future.delayed in your Data class but the error is that you're trying to return a String to a ProceessedData
I would do sonething like this:
是的,那是因为您正在调用
字符串
时,请调用processeddata.ementy()
。您可以将方法
empty
更改为命名构造函数,然后它将正常工作。您的
processeddata
类:将其更改为:
Yes, that's because you are returning a
String
when callingProcessedData.empty()
.You can change the method
empty
to be a named constructor then it will work properly.Your
ProcessedData
class:Change it to: