Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

Flutter saving data dynamically

I want to save data from my TextField in my device. I accomplish this by TextEditingController. But now, I want to do that dynamically. I will have meany TextField's and I only learned how to do it statically. Can someone write demo application showing how I should do it, or explain somehow?

I was trying to find anything on the internet, sadly I didn't

(in the end, I want my application to use this data if I close application or restart phone)

This class is saving data statically, but I think the problem is with controller:

class Storage {
  //gets path of the file
  Future<String> get _localPath async {
    final dir = await getApplicationDocumentsDirectory();
    return dir.path;
  }

  //gets the location of the file
  Future<File> get _localFile async {
    final path = await _localPath;
    return File("$path/db.txt");
  }

  //write to the file
  Future<File> writeData(String message) async {
    final file = await _localFile;
    return file.writeAsString("$message");
  }

  //read from the file
  Future<String> readData() async {
    try {
      final file = await _localFile;
      String body = await file.readAsString();
      return body;
    } catch (e) {
      return e.toString();
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...