Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return multiple values from function in Dart?

Tags:

dart

Say I have:

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

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

like image 699
iDecode Avatar asked Sep 04 '26 23:09

iDecode


1 Answers

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.

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);

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

like image 96
julemand101 Avatar answered Sep 06 '26 20:09

julemand101