How can I save widget's data to device in Flutter?

Question

Asked by blackdemon7 on November 06, 2021 (source).

when a file add I want to do an easy way to access that file in my app. to do that I am using ListView.builder() widget. but I can't save the data in the app I worked on the shared_preferences package. At least I don't know how I must do

Answer

Question answered by blackdemon7 (source).

I solved my problem with this block, I am using ListviewBuilder to create widgets so I needed an array. after this, I wanted to save this data on my device. I am using shared preferences but shared preferences only allow to add string array. then, I decoded all arrays to String array then, when I start the widget in initstate. I take data then, encode the array data then, add value to my variable.

SharedPreferences? sharedPref;
List<Map<String, dynamic>> documents = [];
List<String> documentsEncode = [];

 @override
  void initState() {
    super.initState();
    getInstance();
  }

 Future<void> getInstance() async {
    final sharedPref = await SharedPreferences.getInstance();
    documentsEncode = sharedPref.getStringList(widget.collection!) ?? [];
    documentsEncodeToDecode();
    // main();
  }

 documentsEncodeToDecode() {
    documents.clear();
    documentsEncode.forEach((documentEncode) {
      Map<String, dynamic> document = jsonDecode(documentEncode);
      documents.add(document);
    });
    setState(() {});
  }
DART FLUTTER
SHARE: