如何从Dart中的函数返回多个值? [英] How to return multiple values from function in Dart?

查看:1192
本文介绍了如何从Dart中的函数返回多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有

Function(int x, int y) func = (int x, int y) {
  return (1, 2); // error
};

如何从上述函数实际返回(1、2)?

How to actually return (1, 2) from above function?

推荐答案

Dart中的方法只能返回一个值。因此,如果您需要返回多个值,则需要将它们打包在另一个对象中,例如

Methods in Dart can only return one value. So if you need to return multiple values you need to pack them inside another object which could e.g. be your own defined class, a list, a map or something else.

如果您使用x和y,则可以考虑使用 Point 来自 dart:math 的类:

In your case with x and y you could consider using the Point class from dart:math:

import 'dart:math';

Point<int> func(int x, int y) => Point(x, y);

此处支持在Dart中返回多个值:
https://github.com/dart-lang/language/issues/68

Support for returning multiple values in Dart are a ongoing discussion here: https://github.com/dart-lang/language/issues/68

这篇关于如何从Dart中的函数返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆