1

I want to modify data from a text asset (.txt file) with a function and display it in a Text() widget. myFunction Takes datatype String as parameter and return datatype String.

I've read the documentation. Loading Asset from Image, this didn't worked. I've also tried solution from Suragch's Answer.

This maybe is a case where I should use FutureBuilder but I'm not able to understand how to make it work here (I'm new). I'm going to use another function to modify the data from file and then display.

This is one of the things I tried:

Widget build(BuildContext context) {
  Future<String> aloadAsset(BuildContext context) async {
    return await DefaultAssetBundle.of(context).loadString('assets/help.txt');
  }

  String helps = myFunction(await aloadAsset(context));
  return Scaffold(
    body: Text(helps)
  );
}

When assigning value from await aloadAsset(context) to String, I get these errors: Unexpected text 'await'. & A value of type 'Future<String>' can't be assigned to a variable of type 'String'.

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
Mushaheed Syed
  • 643
  • 10
  • 23

2 Answers2

0

await keyword can only be used in an async function. What you can do here is: Make the widget as StatefulWidget and then in the state class:

String helps = "";

in the initState() method:

aloadAsset();

and change your function to:

aloadAsset() async {
    helps = myFunction(await rootBundle.loadString('assets/help.txt'));
    setState((){});
  }

Don't forget to add the import

import 'package:flutter/services.dart' show rootBundle;
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ryosuke
  • 3,592
  • 1
  • 11
  • 28
0

This is how to read the text from your file, you have to modify your Build function to include a FutureBuilder. Then you have to move the aloadAsset function out of the build:

@override
Widget build(BuildContext context) {
  return FutureBuilder(
      future: aloadAsset(),
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        if(if(snapshot.hasData)){
            return Scaffold(
              body: Text(snapshot.data)
            );
        }
            return Scaffold(
              body: Text('No Available data') //This will be returned in case you didn't receive data yet or in the case of a file error.
            );
      }
}

Future<String> aloadAsset(BuildContext context) async {
     return await DefaultAssetBundle.of(context).loadString('assets/help.txt');

}
Mazin Ibrahim
  • 7,433
  • 2
  • 33
  • 40
  • kindly explain `if(snapshot.connectionState == ConnectionState.done)` vs `if(snapshot.hasData)`. Which is better to use? – Doc Mar 24 '19 at 16:02
  • for your case it is better to use the latter : if(snapshot.hasData) to handle the event of wrong file name or nonexistent one. I will edit the answer. – Mazin Ibrahim Mar 24 '19 at 16:06
  • I am not the OP, just read your answer and wanted to clarify; thanks for the edit. – Doc Mar 25 '19 at 09:25
  • I know, and by the way thank you for notifying me for this issue which led me to correct my answer to suit the post case. – Mazin Ibrahim Mar 25 '19 at 09:27
  • Thanks for the answer. Can you explain how can I assign the data from "assets/help.txt" file to a String? – Mushaheed Syed Mar 25 '19 at 20:49
  • You can define a class-level var and assign the value to it inside `aloadAsset()` – Mazin Ibrahim Mar 26 '19 at 04:52