如何定义返回同一类的对象的函数?

发布于 2025-02-06 07:47:16 字数 1860 浏览 2 评论 0原文

我正在做这个示例教程在异步函数上并查看本节,

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

oО清风挽发oО 2025-02-13 07:47:16

processeddata.empty()的目的是创建一个没有数据的新实例processeddata(不是String)。直接在processeddata类上直接调用它的事实,意味着empty()是静态方法,工厂构造函数或命名的构造函数(这些中的任何一个都将做)。

它还不需要返回Future - 无需等待任何内容即可获取空数据的实例。 createAta是一种异步方法,这意味着 值从该方法中返回processeddata

考虑到所有这些,您可以定义empty这样:

class ProcessedData {
  // ...

  factory ProcessedData.empty() => ProcessedData("");
}

The purpose of ProcessedData.empty() is to create a new instance of ProcessedData (not String) which has no data. The fact that it is called directly on the ProcessedData class, means that empty() 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 that createData is an async method means that any value returned from the method will already be wrapped in a Future, so you can just return a ProcessedData.

With all that in mind, you can define empty like this:

class ProcessedData {
  // ...

  factory ProcessedData.empty() => ProcessedData("");
}
鸵鸟症 2025-02-13 07:47:16

我不明白您为什么要添加未来

class ProceessedData {
  ProceessedData(this.data);

  final String data;

  factory ProceessedData.empty() {
    return ProcessedData('');
  }
}

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:

class ProceessedData {
  ProceessedData(this.data);

  final String data;

  factory ProceessedData.empty() {
    return ProcessedData('');
  }
}
遮云壑 2025-02-13 07:47:16

是的,那是因为您正在调用字符串时,请调用processeddata.ementy()

您可以将方法empty更改为命名构造函数,然后它将正常工作。

您的processeddata类:

class ProcessedData {
    ProcessedData(this.data);

    final String data;

    Future<String> empty() {
        return Future.delayed(const Duration(seconds: 4),
            () => HttpException('http://www.google.com.').toString());
    }
}

将其更改为:

class ProcessedData {
    ProcessedData(this.data);

    final String data;

    ProcessedData.empty() :
        data = HttpException('http://www.google.com.').toString();
}

Yes, that's because you are returning a String when calling ProcessedData.empty().

You can change the method empty to be a named constructor then it will work properly.

Your ProcessedData class:

class ProcessedData {
    ProcessedData(this.data);

    final String data;

    Future<String> empty() {
        return Future.delayed(const Duration(seconds: 4),
            () => HttpException('http://www.google.com.').toString());
    }
}

Change it to:

class ProcessedData {
    ProcessedData(this.data);

    final String data;

    ProcessedData.empty() :
        data = HttpException('http://www.google.com.').toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文