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,
          ),`
        );
      }

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

If I will use future builder I cannot use story items plz help me out here
Why not? I don't know how to help you, without a better problem description.
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
So, make one function to translate the data, since that will return a Future<YourData>, you will need a FutureBuilder to properly build your UI.

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.