1

This is the code I have been trying. I want to return a String using extractInfo() method.

How can we return a String because the parameter is not accepting any other argument than the String

      Future<String> info(String x) a sync{
        final translator = Google Translator();
        return await translator.translate(x, to: 'en');
      }
      String y;
      String extract Info(){
        String x = "Title :${widget.userPosts[widget.index].title}\n"
            "User Id :${widget.userPosts[widget.index].userId}\n"
            "Id :${widget.userPosts[widget.index].id}\n"
            "Completed :${widget.userPosts[widget.index].completed}\n";
    
        info(x).then((value) => y = value);
        print(y);
        return x;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("More"),
          ),
          body: StoryView(
            storyItems: [
              StoryItem.text(
                title: extractInfo(),
                backgroundColor: Colors.redAccent,
              )
            ],
            onStoryShow: (s) {
              print("Showing a story");
            },
            onComplete: () {
              print("Completed a cycle");
            },
            progressPosition: ProgressPosition.top,
            repeat: false,
            controller: story Controller,
          ),`
        );
      }
Andrey Ozornin
  • 1,129
  • 1
  • 9
  • 24
Pranav
  • 11
  • 3

1 Answers1

1

You cannot. You will need to use async/await like you did in the first method. Since your control does not accept a Future<String> you will need to wrap it in a FutureBuilder. You can see how to do that here

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • If I will use future builder I cannot use story items plz help me out here – Pranav Jul 02 '20 at 12:57
  • Why not? I don't know how to help you, without a better problem description. – nvoigt Jul 02 '20 at 13:12
  • We would like to show our data as in instagram story. We are translating the data recieved frm the api. Everything is working but this translated data we are not able to show – Pranav Jul 06 '20 at 06:26
  • So, make one function to translate the data, since that will return a `Future`, you will need a `FutureBuilder` to properly build your UI. – nvoigt Jul 06 '20 at 06:27