Ad

Flutter Loading Data From SharedPreference Before The Page Loads

- 1 answer

I pull more than one data from ListString and then separate them with a For Loop, make an object and then add it to a list and add this list to another list and DropDownList adds elements in that list. But the problem is when i trying to adding list(object list) to the other list(DropDownList list) I can't add elements.

code:

    late String selectedValue = '';
  List<String> dropDownItemValue = [];
  List<NotePages> notePages = [];
  final List<NotePages> pages = [];

  Future<NotePages?> read() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    var keys = prefs.getKeys();

    for (var key in keys) {
      var pageList = prefs.getStringList(key);

      if (pageList?[0] == 'page') {
        NotePages page = new NotePages(
            type: pageList![0],
            pageName: pageList[1],
            createdTime: DateTime.parse(pageList[2]));

        pages.add(page);
        await addInTheList();
      }
    }
  }

  addInTheList() {
    for (var i = 0; i < pages.length; i++) {
      notePages.add(pages[i]);
      print(pages[i]);
    }
  }

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

    read();

    for (var i = 0; i < notePages.length; i++) {
      dropDownItemValue.add(notePages[i].pageName);
    }
    dropDownItemValue.add('');
    dropDownItemValue.add('Add Page');
    print(dropDownItemValue);
    // selected value must be contain at dropDownItemValue
    selectedValue = dropDownItemValue[0];
  }

When i use Then() in the initState the list adding elements to the other list i mean its working (i can see when i print the last list) but i can't open the DropDownList it acts like he has no element. Code like this:

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

    read().then((_) => {
          for (var i = 0; i < pages.length; i++) {notePages.add(pages[i])},

          for (var i = 0; i < notePages.length; i++)
            {dropDownItemValue.add(notePages[i].pageName)},
          dropDownItemValue.add(''),
          dropDownItemValue.add('Add Page'),
          print(dropDownItemValue),
          // selected value must be contain at dropDownItemValue
          selectedValue = dropDownItemValue[0],
        });
  }

how can i solve this?

Ad

Answer

I recommend using GetStorage

https://pub.dev/packages/get_storage

it's realtime so you won't need any async or future builder or anything.

just add this line to your main function:

void main() async {
  await GetStorage.init();
  runApp(MyApp());
}

and then use it like charm...

void setData(String key, dynamic value) => GetStorage().write(key, value);

int? getInt(String key) => GetStorage().read(key);

String? getString(String key) => GetStorage().read(key);

bool? getBool(String key) => GetStorage().read(key);

double? getDouble(String key) => GetStorage().read(key);

dynamic getData(String key) => GetStorage().read(key);

void clearData() async => GetStorage().erase();
Ad
source: stackoverflow.com
Ad