0

I'm writing a Application, where Widgets and their State need to be saved to Disk and later be restored. In order to save a StatefulWidget I need to access it's corresponding State<T> object.

Here's how I imagined the code to look like:

class Block extends StatefulWidget {
  const Block({Key? key}) : super(key: key);

  void saveToDisk(){
    // access BlockState object
    // save to disk…
  }

  @override
  BlockState createState() => BlockState();
}

class BlockState extends State<Block> {
  final String _someState = ‚Hello Stackoverflow‘;

  @override
  Widget build(BuildContext context) {
    return const Text(‚Some Text‘);
  }
}

Does anybody know how to access the BlockState object (first comment in saveToDisk())?

Luke
  • 365
  • 3
  • 10
  • 1
    most likely you need [RestorationMixin](https://api.flutter.dev/flutter/widgets/RestorationMixin-mixin.html) mixin - the docs say: *"Manages the restoration data for a State object of a StatefulWidget. Restoration data can be serialized out and, at a later point in time, be used to restore the stateful members in the State object to the same values they had when the data was generated."* – pskink Dec 19 '21 at 13:13
  • @pskink thanks a lot! This looks like what I was looking for. – Luke Dec 19 '21 at 18:01
  • sure, your welcome – pskink Dec 19 '21 at 18:55

1 Answers1

0

widget.saveToDisk();

All stateful widgets have it this way.

Fred Grott
  • 3,505
  • 1
  • 23
  • 18