Home > front end >  How can I return a list string from a Future
How can I return a list string from a Future

Time:11-08

I have a future call that I want to get 2 list Strings

But I get the return value it's a Future<List< dynamic >>

var datasource = const FutureInformation();

    getSliderDetailsEvents() async {
      SliderDetails futureEvents = await datasource.getSliderDetails();
      List<String> images = futureEvents.image; //List<String>
      List<String> titles = futureEvents.title; // List<String>

      List information = [images,titles]; //return an array
      return  information;
    }

How can I access the two List in this async call? I know as it's async I have to return a future, Would I have to create a FutureBuilder to create the List's.

Thanks

CodePudding user response:

I answer here to format response better, you can do something like this from your code:

Future<List<String>> getSliderDetailsEvents() async {
      SliderDetails futureEvents = await datasource.getSliderDetails();
      List<String> images = futureEvents.image; //List<String>
      List<String> titles = futureEvents.title; // List<String>

      List<String> information = [...images,...titles]; //
      return  information;
    }

CodePudding user response:

By using the Spread Operator ...

Future<List<String>> getSliderDetailsEvents() async{
   SliderDetails futureEvents = await datasource.getSliderDetails();
      final images = futureEvents.image;
      final titles = futureEvents.title;
      return [...images, ...titles];
}

or you can do this like this

Future<List<String>> getSliderDetailsEvents() async{
  SliderDetails futureEvents = await datasource.getSliderDetails();
  return [...futureEvents.image, ...futureEvents.title];
}

CodePudding user response:

You may want to specify the type of objects that your list is storing (I assume you're creating a list of lists in your code):

  List<List<String>> information = <List<String>>[images,titles]; //return an array
  • Related