无法从捕获的文本文件中捕获翻译后的文本

Unable to capture the translated text from the captured text file

我试图从文本文件中捕获文本,然后调用翻译函数来翻译捕获的文本。 我成功地从文件中捕获了文本,但无法将其翻译成另一种语言。 使用过该插件,但我对设置状态部分感到困惑。 看看代码。

class TextFile extends StatefulWidget {
  @override
  _TextFileState createState() => _TextFileState();
}

class _TextFileState extends State<TextFile> {
  String data = "";
  getFileData() async {
    String responseText;
    responseText = await rootBundle.loadString('files/cart.txt');
    setState(() {
      data = responseText;
    });
  }

  @override
  void initState() {
    getFileData();
    textTranslate();
    super.initState();
  }

  GoogleTranslator translator = GoogleTranslator();

  //translate function
  String out;
  textTranslate() {
    // Receiving a red underline in the output region stating:

   // A value of type 'Translation' can't be assigned to a variable of type 
    'String'.
    Try changing the type of the variable, or casting the right-hand type to 
    'String'.dartinvalid_assignment

    translator.translate(data, to: "es").then((output) {
      setState(() {});
      
    });
    print(output);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Text Extract'),
      ),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(15.0),
            child: Text(data),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.white,
        onPressed: () {
          textTranslate();
        },
        child: Icon(Icons.g_translate, color: Colors.black, size: 30),
      ),
    );
  }
}

请帮我解决这个问题

这应该适合你

class TextFile extends StatefulWidget {
  @override
  _TextFileState createState() => _TextFileState();
}

class _TextFileState extends State<TextFile> {
  String data = "";
  getFileData() async {
    String responseText;
    responseText = await rootBundle.loadString('files/cart.txt');
    data = responseText;
    setState(() {});
    await textTranslate();
  }

  @override
  void initState() {
    super.initState();
    getFileData();
  }

  GoogleTranslator translator = GoogleTranslator();

  //translate function
  String out;
  textTranslate() async {
    var output = await translator.translate(data, to: "es");
    data = output.text; // set the translated text to 'data'
    setState(() {});
    print(output.text); // translated text should be here
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Text Extract'),
      ),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(15.0),
            child: Text(data),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.white,
        onPressed: () => textTranslate(),
        child: Icon(Icons.g_translate, color: Colors.black, size: 30),
      ),
    );
  }
}