Return multiple values from function

前端 未结 4 511
抹茶落季
抹茶落季 2021-01-01 08:28

Is there a way to return several values in a function return statement (other than returning an object) like we can do in Go (or some other languages)?

For example,

4条回答
  •  难免孤独
    2021-01-01 09:21

    Create a class:

    import 'dart:core';
    
    class Tuple {
      final T1 item1;
      final T2 item2;
    
      Tuple({
        this.item1,
        this.item2,
      });
    
      factory Tuple.fromJson(Map json) {
        return Tuple(
          item1: json['item1'],
          item2: json['item2'],
        );
      }
    }
    

    Call it however you want!

    Tuple(i1, i2);
    or
    Tuple.fromJson(jsonData);
    

提交回复
热议问题