0

So I was sitting for around two days trying to save a small list using shared preferences. In the code below you can see a many loops which I know isn't very efficient but they work fine for my case so no need to look into them. After I finish using the loops for my algorithm I want to save the output for next time usage. The list which I want to save is "list5". I tried saving it like this but it didn't work out for me. Also I have a problem with ".isNotEmpty" returning a null error.

Thank you in advance.

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Result extends StatelessWidget {
  List<String> people1;
  List<bool> peoplePres;
  Result({this.people1, this.peoplePres});
  List<String> lastPres;
  void initState() {
    lastZakif();
  }

  void lastZakif() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    lastPres = (prefs.getStringList('lastPres') ?? 0);
  }

  void _loadingNew(List<String> list5) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    lastPres = list5;
    await prefs.setStringList('lastPres', lastPres);
  }

  @override
  Widget build(BuildContext context) {
    int count = 0; //count for how many people are here
    int count2 = 1; //count for the new order
    int count4 = 0; //count for last schedule
    List<int> list1 = new List(people1.length); //List with the algorithm
    List<int> list2 = new List(people1.length); //List with the new order
    List<String> list4 = new List(people1.length); // for loading last schedule
    if (lastPres.isNotEmpty==true) {
      for (int i = 0; i < people1.length; i++) {
        //loading the last schedule and adding people if neccesary part 1
        for (int j = 0; j < people1.length; j++) {
          if (people1[j] == lastPres[i]) {
            list4[count4] = lastPres[i];
            count4++;
          }
        }
      }
      bool check1 = false; //true if person exists
      for (int i = 0; i < people1.length; i++) {
        //loading the last schedule and adding people if neccesary part 2

        for (int j = 0; j < people1.length; j++) {
          if (people1[i] == list4[j]) {
            check1 = true;
          }
        }
        if (check1 == false) {
          list4[count4] = people1[i];
          count4++;
          check1 = false;
        }
      }
      people1 = list4;
    }
    for (int i = 0; i < people1.length; i++) {
      //counting People that are here
      if (peoplePres[i] == true) {
        count++;
      }
    }
    if (count == people1.length) {
      count2 = 0;
    }

    for (int i = 0; i < people1.length; i++) {
      // Declaring a list which will be the index for the next Zkifut
      list1[i] = i;
    }

    for (int i = 0; i < people1.length; i++) {
      //Applying the algorithm
      int num1 = count ~/ 3;
      if (count % 3 == 2) {
        num1 += 1;
      }
      list1[i] = list1[i] + num1 - count;
    }

    for (int i = 0; i < people1.length; i++) {
      // making the new schedule for absent people but starting with 0
      if (peoplePres[i] == false) {
        list2[i] = 0;
        break;
      }
    }

    for (int i = 0; i < people1.length; i++) {
      // makeing the new schedule pos num
      if ((list1[i] >= 0) && (peoplePres[i] == true)) {
        list2[i] = count2;
        count2++;
      }
    }

    for (int i = 0; i < people1.length; i++) {
      // makeing the new schedule neg num
      if ((list1[i] < 0) && (peoplePres[i] == true)) {
        list2[i] = count2;
        count2++;
      }
    }
    for (int i = 0; i < people1.length; i++) {
      // makeing the new schedule for absent people
      if ((peoplePres[i] == false) && (list2[i]) != 0) {
        list2[i] = count2;
        count2++;
      }
    }

    int count3 = 0;
    List<String> list3 = new List(count);
    for (int i = 0; i < people1.length; i++) {
      if (peoplePres[list2[i]] == true) {
        list3[count3] = people1[list2[i]];
        count3++;
      }
    }
    List<String> list5 = new List(people1.length); // for saving algorithm
    for (int i = 0; i < people1.length; i++) {
      list5[i] = people1[list2[i]];

    }

    _loadingNew(list5);

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          'Result',
          style: TextStyle(fontSize: 30),
        ),
      ),
      body: ListView.builder(
          itemCount: count,
          itemBuilder: (context, value) {
            return Card(
              color: Colors.amberAccent[200],
              elevation: 3,
              child: Container(
                child: ListTile(
                  leading: Text('${value + 1}'),
                  title: Text(list3[value]
                      //people1[value],
                      ),
                ),
              ),
            );
          }),
    );
  }
}
Dan7nm
  • 51
  • 8

1 Answers1

0

This is how you can save your list in SharedPreferences as below:

void _loadingNew(List<String> list5) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
        SharedPreferences.Editor editor = prefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(list5);
        editor.putString(key, json);
        editor.apply();     // This line is IMPORTANT !!!

  }

Reference : https://gist.github.com/WrathChaos/5f39e3ce3874a049d25e2ca8958d18b6

Also about isNotEmpty() As per answer given by @gprasant Check whether a string is not null and not empty

The isNotEmpty() checks only that the input parameter is

  1. not null
  2. not the Empty String ("")

Best way to check if the list is empty or not is:

If you want to so something only when the list is empty, it is safer to write :

if (list != null && list.isEmpty()) { do something }

If you want to do something if the list is either null or empty, you can write :

if (list == null || list.isEmpty()) { do something }

If you want to do something if the list is not empty, you can write :

if (list != null && !list.isEmpty()) { do something }

Reference : Two ways to check if a list is empty - differences?

Hope that helps.

Omkar C.
  • 755
  • 8
  • 21
  • why should I use ArrayList and whats the difference ? – Dan7nm Jan 04 '20 at 16:14
  • You see I only need to save a String List which is no problem for using shared preferences I don't think there is a reason to use arrayList – Dan7nm Jan 04 '20 at 17:59
  • You're right @Dan7nm you can use list as well, for more reference on this see this page - http://androidopentutorials.com/android-how-to-store-list-of-values-in-sharedpreferences/ They have shown how to save data in Sharedpreference using list. Let me know if you still have any queries. – Omkar C. Jan 04 '20 at 18:08
  • Just for knowledge, refer this page - https://stackoverflow.com/questions/14903145/what-is-the-difference-between-list-and-arraylist for answer on difference between list and arraylist and what you could achieve implementing both. – Omkar C. Jan 04 '20 at 18:11
  • @Dan7nm Did my Answer help you? – Omkar C. Jan 05 '20 at 19:23
  • Not really, it only converts to array list but the result is the same . – Dan7nm Jan 06 '20 at 07:23
  • Did you manage to refer this http://androidopentutorials.com/android-how-to-store-list-of-values-in-sharedpreferences/ as it should help you saving data as list in Sharedpreferences. – Omkar C. Jan 06 '20 at 09:05