Kim San
Kim San

Reputation: 805

Return multiple value from function in dart

Hi so i'm new to dart and I'm having an issue with returning 2 value from a dart function.

Currently I have this function :

  Future LoadAllData({required Map data, required String detailId}) async {
    loadUserData(data: data);

    powData = await Database.getPowDataActive(detailId: detailId);

    return powData;
  }

so getPowDataActive is a function that will fetch a data from my database and it will return some map data, load user data will also fetch data and will also return some map. I wanted to use the loadAllData function for my futureBuilder and use the snapshot data from this 2 function for different purposes, can I do so ? or I have to first combine the return from both function into 1 variable and access it differently ?

Thanks before

Upvotes: 0

Views: 534

Answers (1)

eamirho3ein
eamirho3ein

Reputation: 17880

You can create a model like this:

class LoadDataResult {
  final Map userData;
  final Map powData;
  LoadDataResult({@requierd this.userData, @requierd this.powData, });
}

and then use it like this:

Future<LoadDataResult> LoadAllData({required Map data, required String detailId}) async {
    var userData = await loadUserData(data: data);

    powData = await Database.getPowDataActive(detailId: detailId);

    return LoadDataResult(userData:userData, powData: powData);
  }

and then use it like this in futureBuilder:

LoadDataResult data = snapshot.data;
print('${data. userData}');

Upvotes: 2

Related Questions