0

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

1 Answer 1

2

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}');
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need the "as" if you properly provided the datatype in the generic parameter for the FutureBuilder, which you should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.