title: How to read file from file to flutter?
body: <p>I have code that I use to write data to a file (such as global settings for an application). I write down the data and see. But the problem is that I do not understand how to take these mini data from another page. For example I wrote the word &quot;Test&quot; and I want and I want to assign that word to some variable in another page. I will be grateful for your help. Here is my code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import 'dart:io';
  import 'dart:async';
  
  import 'package:path_provider/path_provider.dart';
  
  import 'package:flutter/material.dart';
  import 'package:flutter/foundation.dart';
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: "Reading and Writing to Storage",
        home: Home(
          storage: Storage(),
        ),
      );
    }
  }
  
  class Home extends StatefulWidget {
    final Storage storage;
  
    Home({Key key, @required this.storage}) : super(key: key);
  
    @override
    HomeState createState() =&gt; HomeState();
  }
  
  class HomeState extends State&lt;Home&gt; {
    TextEditingController controller = TextEditingController();
    String state;
    Future&lt;Directory&gt; _appDocDir;
  
    @override
    void initState() {
      super.initState();
      widget.storage.readData().then((String value) {
        setState(() {
          state = value;
        });
      });
    }
  
    Future&lt;File&gt; writeData() async {
      setState(() {
        state = controller.text;
        controller.text = '';
      });
  
      return widget.storage.writeData(state);
    }
  
    void getAppDirectory() {
      setState(() {
        _appDocDir = getApplicationDocumentsDirectory();
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Reading and Writing Files'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: &lt;Widget&gt;[
              Text('${state ?? "File is Empty"}'),
              TextField(
                controller: controller,
              ),
              RaisedButton(
                onPressed: writeData,
                child: Text('Write to File'),
              ),
              RaisedButton(
                child: Text("Get DIR path"),
                onPressed: getAppDirectory,
              ),
              FutureBuilder&lt;Directory&gt;(
                future: _appDocDir,
                builder:
                    (BuildContext context, AsyncSnapshot&lt;Directory&gt; snapshot) {
                  Text text = Text('');
                  if (snapshot.connectionState == ConnectionState.done) {
                    if (snapshot.hasError) {
                      text = Text('Error: ${snapshot.error}');
                    } else if (snapshot.hasData) {
                      text = Text('Path: ${snapshot.data.path}');
                    } else {
                      text = Text('Unavailable');
                    }
                  }
                  return new Container(
                    child: text,
                  );
                },
              )
            ],
          ),
        ),
      );
    }
  }
  
  class Storage {
    Future&lt;String&gt; get localPath async {
      final dir = await getApplicationDocumentsDirectory();
      return dir.path;
    }
  
    Future&lt;File&gt; get localFile async {
      final path = await localPath;
      return File('$path/db.txt');
    }
  
    Future&lt;String&gt; readData() async {
      try {
        final file = await localFile;
        String body = await file.readAsString();
  
        return body;
      } catch (e) {
        return e.toString();
      }
    }
  
    Future&lt;File&gt; writeData(String data) async {
      final file = await localFile;
      return file.writeAsString("$data");
    }
  }</code></pre>
  </div>
  </div>
  </p>
  <p>And scrin:
  <a href="https://i.stack.imgur.com/E3jtU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E3jtU.jpg" alt="enter image description here" /></a></p>