0

I'm trying to load different values from a .txt file line by line.

I'm loading an file from assets, which is already registered in pubspec.yaml.

Future<String> getFileData() async {
  return await rootBundle.loadString('assets/files/text.txt');
}

As this returns one long String I split it into a list:

void doStuff() async {
  await getFileData().then((value) => value.split('\n').forEach((element) {
        exampleQuestions.add(element);
      }));
}

This method is called in my class - constructor which is called from my Main by a named route:

  GameOver.routeName: (context) => GameOver(),
    '/game': (context) => Game(

        ),
  },

exampleQuestion get's filled in the doStuff() - method (already checked that), but looses it's values in the _GameState class where the widget, that want's to access the list returns an out of index exception.

However if i quick reload my application the values are set correctly. If I then click the button to continue again nothing changes until I quick reload.

Any hints are very appreciated ..

1
  • try to use setState method when you add elements to the list Commented Apr 26, 2021 at 0:43

1 Answer 1

1

You should not call asynchronous methods like this in widget constructors, because they'll be called on every build.

You should use a StatefulWidget with a FutureBuilder instead.

On another note, LineSplitter should be used for splitting lines.

class _MyWidgetState extends State<MyWidget> {
  late final Future<List<String>> _exampleQuestionsFuture;

  static Future<String> _getFileData() => rootBundle.loadString('assets/files/text.txt');

  @override
  void initState() {
    super.initState();
    _exampleQuestionsFuture = _getFileData().then(const LineSplitter().convert);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<String>>(
      future: _exampleQuestionsFuture,
      builder: (snapshot, data) {
        /* ... */
      },
    );
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. How can I read from the Future<List<String>> now ? I tried something like: @override void initState(){ super.initState(); _getFileData().then(const LineSplitter().convert).then((value) { setState(() { question = value.elementAt(currentRound); }); }); but from my understainding this makes no sense, because the value is only updated once.
question: _exampleQuestionsFuture.then((value) => value.elementAt(currentRound)) , returns with cannot applie Future<String> to String
Use the snapshot in FutureBuilder. FutureBuilder is made for single-use Futures.
Great! Be sure to mark this answer as accepted if it worked for you.
How can i get the length of the _exampleQuestionsFuture now? I tried different approches, but always run into the problem , that at some point i cannot convert Future<int> to int

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.