Search code examples
fluttersharedpreferences

Displaying Data in a Table with Shared Preferences Issue


"I have one record in Shared Preferences, and I've created a table using the Table class. My goal is to display these records in a table format with name, email, and score, but the code is not working correctly, table is empty" How can i fix this problem.Thank in advance.....

this is shared preference file

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="flutter.User_aaa_name">aaa</string>
    <string name="flutter.User_aaa_score">4</string>
    <string name="flutter.User_aaa_email">digrev@gmail.com</string>
    <string name="flutter.User_aaa_password">123</string>
</map>

this my code

 List<User> users = [];

  @override
  void initState() {
    super.initState();
    // Shared Preferences'tan bütün kayıtları al
    _getUsersData();
  }

  Future<void> _getUsersData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<String> keys = prefs.getKeys().toList();

    for (String key in keys) {

      if (key.startsWith('User_')) {
        String userData = prefs.getString(key) ?? 'xxx';
        List<String> userDataList = userData.split(',');


        if (userDataList.length == 3) {
          users.add(User(
            name: userDataList[0],
            score: userDataList[1],
            email: userDataList[2],
          ));
        }
        print("length "+userDataList.length.toString());
      }
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tüm Kayıtları Göster'),
      ),
      body: Center(
        child: Table(
          border: TableBorder.all(),
          children: [
            TableRow(
              children: [
                TableCell(
                  child: Center(child: Text('Adı')),
                ),
                TableCell(
                  child: Center(child: Text('Puanı')),
                ),
                TableCell(
                  child: Center(child: Text('E-posta')),
                ),
              ],
            ),
            for (User user in users)
              TableRow(
                children: [
                  TableCell(
                    child: Center(child: Text(user.name)),
                  ),
                  TableCell(
                    child: Center(child: Text(user.score)),
                  ),
                  TableCell(
                    child: Center(child: Text(user.email)),
                  ),
                ],
              ),

Solution

  • You can try this solution.

    () async {
              SharedPreferences prefs = await SharedPreferences.getInstance();
              log(prefs.getKeys().map((e) => e.runtimeType));
              List<String> keys = prefs.getKeys().toList();
              log(keys);
              List<Object> userDataList = [];
              for (String key in keys) {
                Object? userData = prefs.get(key);
                userDataList.add(userData!);
              }
              log("userDataList ${userDataList.length}");
               {
                users.add(User(
                  name: userDataList[0],
                  score: userDataList[1],
                  email: userDataList[2],
                ));
                print("length " + userDataList.length.toString());
              }
              return;
            }
    

    You can add the condition in between the loop.