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],
),
),
),
);
}),
);
}
}