0

Hi how i can Load this list in a ListView or ListViebuilder?

 Future<List<bool>> getBoolList() async{
  List<bool> prefList = [];
  var sharedPreferences = await SharedPreferences.getInstance();
  Set<String> keys = sharedPreferences.getKeys();

  for(int i=0; i<keys.length ; i++){
    bool value = sharedPreferences.getBool(keys.elementAt(i));
    prefList.add(value);
  }
  
  return prefList;
}

List<bool> list = await getBoolList();

how I got there Flutter SharedPreferences how to load all saved?

Edit: my favorite.dart

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

// ignore: must_be_immutable
class Favoriten extends StatefulWidget {
  @override
  _FavoritenState createState() => _FavoritenState();
}

class _FavoritenState extends State<Favoriten> {



  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('Favorites'),
      ),
      body: // MyList
    );
  }
}
Marcel
  • 75
  • 1
  • 9

1 Answers1

0

You have to use stream builder. It observes the stream. (Best way to implement is using bloc pattern)

class Favoriten extends StatefulWidget {
  @override
  _FavoritenState createState() => _FavoritenState();
}

class _FavoritenState extends State<Favoriten> {

  final _boolList = PublishSubject<List<bool>>();
  Observable<List<bool>> get boolList => _boolList.stream;

  loadList() async{
    List<bool> prefList = [];
    var sharedPreferences = await SharedPreferences.getInstance();
    Set<String> keys = sharedPreferences.getKeys();

    for(int i=0; i<keys.length ; i++){
      bool value = sharedPreferences.getBool(keys.elementAt(i));
      prefList.add(value);
    }

    _boolList.sink.add(prefList);
  }

  @override
  Widget build(BuildContext context) {
    loadList();
    return StreamBuilder(
          stream: boolList,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return root(snapshot.data);
            } else {
              return Container(
                child: YourLoader(),// display loader
              );
            }
          }
      );
  }

  Widget root(List<bool> list){
    return ListView.builder(
      itemBuilder: (context, index) {
         return Container(); // your design here
      }
      itemCount: list.length,
    );
  }
}

Note :- You have to add rxdart: ^0.22.0 plugin in your pubspec.yaml and then import 'package:rxdart/rxdart.dart';

Arpit Awasthi
  • 493
  • 2
  • 8
  • I have a lot of mistakes when I insert this :( how do i add this to my page? I added my page to my question – Marcel Oct 27 '20 at 22:40
  • can you still help me? – Marcel Oct 28 '20 at 13:13
  • @Marcel you need to cover some basics before you jump into all this. I will recommend you to go through some tutorials. – Arpit Awasthi Oct 28 '20 at 15:42
  • Thanks, I am currently studying, but can you still help me with the problem? – Marcel Oct 28 '20 at 15:49
  • Add the code above in your _FavoritenState class. Wrap StreamBuilder inside Scaffold. – Arpit Awasthi Oct 28 '20 at 15:59
  • The method 'PublishSubject' isn't defined ---- Undefined class 'Observable' ---- Providing a non-null itemCount improves the ability of the [ListView]----- The method 'YourLoader' isn't defined for the type '_FavoritenState' have thi errors – Marcel Oct 28 '20 at 16:03
  • why don't you help me to the end? there is not much left to do – Marcel Oct 29 '20 at 08:57
  • rxdart: ^0.22.0, use this plugin. This will remove - 'PublishSubject' isn't defined, Undefined class 'Observable'. I wrote 'YourLoader' for you to show the progress bar which you are using for your project – Arpit Awasthi Oct 29 '20 at 17:38
  • dont understand what you mean with Observable can you show me the complete code with my favorite.dart? – Marcel Oct 29 '20 at 19:24
  • i see you have edit your post but i still have mistakes on Observable (Undefined class 'Observable') on YourLoader (The method 'YourLoader' isn't defined for the type '_FavoritenState'.) and on itemCount (Expected to find ','.) – Marcel Oct 29 '20 at 20:58
  • and that is missing scatfold in your example – Marcel Oct 29 '20 at 20:59
  • can someone help me? – Marcel Oct 30 '20 at 14:17
  • Hey @Arpit Awasthi can you still help me? – Marcel Nov 03 '20 at 23:20