Browse Source

added multi person serialization

Ray-works 9 months ago
parent
commit
4f2ac12d2f
1 changed files with 22 additions and 9 deletions
  1. 22 9
      lib/startseite.dart

+ 22 - 9
lib/startseite.dart

@@ -11,6 +11,8 @@ class StartSeite extends StatefulWidget {
11 11
 }
12 12
 
13 13
 class _StartSeiteState extends State<StartSeite> {
14
+  List<Person> generatedPersons = List<Person>();
15
+
14 16
   Future<String> fetchPerson() async {
15 17
     final person = await http.get('https://randomname.de/?format=json&count=1');
16 18
     dynamic json = "";
@@ -36,14 +38,16 @@ class _StartSeiteState extends State<StartSeite> {
36 38
 
37 39
   void printPreferences() async {
38 40
     SharedPreferences prefs = await _prefs;
39
-    print(prefs.getInt("anzahlPersonen"));
40
-
41
-    String personjson = prefs.getString("Person");
42
-    Person p = Person.fromJson(personjson);
43
-    print("${p.firstname} ${p.lastname}");
41
+    setState(() {
42
+      List<String> personenliste = List<String>();
43
+      personenliste = prefs.getStringList("Personenliste");
44
+        for(String pjson in personenliste){
45
+          print("Reading: $pjson");
46
+          generatedPersons.add(Person.fromJson(pjson));
47
+        }
48
+    });
44 49
   }
45 50
 
46
-  List<Person> generatedPersons = List<Person>();
47 51
   final listScrollController = ScrollController();
48 52
 
49 53
   GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
@@ -95,10 +99,19 @@ class _StartSeiteState extends State<StartSeite> {
95 99
 
96 100
                       setState(() {
97 101
                         generatedPersons.add(p);
98
-                        prefs.setInt("anzahlPersonen", generatedPersons.length);
99
-                        prefs.setString("Person", p.toJson());
102
+
103
+                        List<String> personenliste = List<String>();
104
+
105
+                        for(Person pobj in generatedPersons){
106
+                          print("Writing: ${pobj.toJson()}");
107
+                          personenliste.add(pobj.toJson());
108
+                        }
109
+
110
+                        prefs.setStringList("Personenliste", personenliste);
111
+
100 112
                         listScrollController.jumpTo(
101 113
                             listScrollController.position.maxScrollExtent);
114
+
102 115
                       });
103 116
                       
104 117
                     }
@@ -154,7 +167,7 @@ class _StartSeiteState extends State<StartSeite> {
154 167
                   SharedPreferences prefs = await _prefs;
155 168
                   setState(()  {
156 169
                     generatedPersons.clear();
157
-                    prefs.setInt("anzahlPersonen", 0);
170
+                    prefs.setStringList("Personenliste", List<String>());
158 171
                   });
159 172
                 },
160 173
                 child: Text("Liste leeren")