0

In my main.dart I have StatefulWidget and class extends State and functions to write and read string to local file:

  Future<void> _writeDataToFile(favs) async {
    final _dirPath = await _getDirPath();
    final _myFile = File('$_dirPath/fav.txt');
    await _myFile.writeAsString("||${_myVariable}||");
  }

  Future<void> _readData() async {
    final dirPath = await _getDirPath();
    final myFile = File('$dirPath/fav.txt');
    final data = await myFile.readAsString(encoding: utf8);

    setState(() {
      _content = data;
    });
  }

I run widget from separated file with:

itemBuilder: (context, index) => allRadioList(articles[index], context, _content),

How Can i run _readData() with state change from allRadioList widget.

Here's my allRadioList(articles[index], context, _content) code:

Widget allRadioList(Article article, BuildContext context, String contentt) {
  return Card(
      child: ListTile(
    title: Text(article.title),
    subtitle: Text(contentt),
    leading: CircleAvatar(backgroundImage: NetworkImage("https://image.tmdb.org/t/p/w300/${article.urlToImage}")),
    trailing: IconButton(
      icon: contentt.contains(article.id.toString()) ? Icon(Icons.star_border_outlined, color: Colors.yellow) : Icon(Icons.star_border_outlined, color: Colors.white),
      tooltip: 'My Tootltip',
      onPressed: () {
        _writeDataToFile(article.id.toString());
        _readData();
      },
    ),
  ));
}
3
  • if you please elaborate more.. what you want..?? Commented Mar 22, 2022 at 10:37
  • In child widget: Widget allRadioList(Article article, BuildContext context, String contentt) I run function to write string to file. But how can I update the state from separated widget file? Commented Mar 22, 2022 at 11:02
  • I have answered similar question here stackoverflow.com/questions/70844227/… So it's relevant to this one also. Commented Mar 22, 2022 at 11:59

2 Answers 2

1

Functions are objects, so you can pass them as function parameter and call it in your function

// (){} is a anonymous function (with no name)
itemBuilder: (context, index) => allRadioList(articles[index], context, _content,onPressed: (){
        _writeDataToFile(articles[index].id.toString());
        _readData();
}),

and

Widget allRadioList(Article article, BuildContext context, String contentt,{Funtion onPressed}) {
  return Card(
      child: ListTile(
    title: Text(article.title),
    subtitle: Text(contentt),
    leading: CircleAvatar(backgroundImage: NetworkImage("https://image.tmdb.org/t/p/w300/${article.urlToImage}")),
    trailing: IconButton(
      icon: contentt.contains(article.id.toString()) ? Icon(Icons.star_border_outlined, color: Colors.yellow) : Icon(Icons.star_border_outlined, color: Colors.white),
      tooltip: 'My Tootltip',
      onPressed: onPressed,
    ),
  ));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the are no errors, but file is not written onPressed. Idea why?
@meetSolo Sorry I didn't get what are you saying.
1

Did you try make async the onPressed function and call these functions with await? Like;

onPressed: () async {
    await _writeDataToFile(article.id.toString());
    await _readData();
  },

Comments

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.