如何在 flutter 中从 Listtile 的地图中检索数据

发布于 2025-01-20 09:05:00 字数 887 浏览 0 评论 0原文

我想从我从共享preference收到的地图中接收数据。

数据看起来像这样:

{2022-04-08 15:49:41.864929:1234567,2022-04-08 15:49:55.392684:1234567,2022-2-2022-04-08 15:50:50:50:17.168655:17.168655:12334567,20222-2-04 08 15:50:39.263044:1234567}

日期是键,数字为值。 现在,我需要在4个列表中显示所有4个键和值。 怎么可能? 我真的很新鲜。


  _saveData() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setString(DateTime.now().toString(), '1234567');
  }

  _clearData() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    await preferences.clear();
    print('Daten gelöscht');
  }



  _loadData() async {
    final prefs = await SharedPreferences.getInstance();
    final keys = prefs.getKeys();

    final prefsMap = Map<String, dynamic>();
    for(String key in keys) {
      prefsMap[key] = prefs.get(key);
    }

    print(prefsMap);
  }```

i want to receive data from a map i received from sharedpreference.

the Data looks like this:

{2022-04-08 15:49:41.864929: 1234567, 2022-04-08 15:49:55.392684: 1234567, 2022-04-08 15:50:17.168655: 1234567, 2022-04-08 15:50:39.263044: 1234567}

The date is they key, and the numbers the value.
Now i need to display all 4 keys and the values in 4 listtiles.
How is this possible?
I am really new in flutter.


  _saveData() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setString(DateTime.now().toString(), '1234567');
  }

  _clearData() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    await preferences.clear();
    print('Daten gelöscht');
  }



  _loadData() async {
    final prefs = await SharedPreferences.getInstance();
    final keys = prefs.getKeys();

    final prefsMap = Map<String, dynamic>();
    for(String key in keys) {
      prefsMap[key] = prefs.get(key);
    }

    print(prefsMap);
  }```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笛声青案梦长安 2025-01-27 09:05:00

我在这里包括一个粗略的例子。
您可以在

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    // An example of using Future to return Map<String, dynamic>
    Future<Map<String, dynamic>> _loadData() async {
      Map<String, dynamic> dataFromSharedPref = {};

      // A task that needs some time to load e.g. Getting data from SharedPreference
      // TODO: Change this part to load from your SharedPreference
      await Future.delayed(const Duration(seconds: 1), () {
        dataFromSharedPref = {
          "2022-04-08 15:49:41.864929": "1234567",
          "2022-04-08 15:49:55.392684": "1234567"
        };
      });

      return dataFromSharedPref;
    }

    return FutureBuilder(
        future: _loadData(),
        builder: (context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            // Show loading when there is no data
            return const Center(child: CircularProgressIndicator());
          } else {
            // Build the list when data is received
            var myData = snapshot.data;
            return ListView.builder(
                itemCount: myData.length,
                itemBuilder: (BuildContext context, int index) {
                  String key = myData.keys.elementAt(index);
                  return ListTile(
                    title: Text('Key: $key'),
                    subtitle: Text('Value: ${myData[key]}'),
                  );
                });
          }
        });
  }
}

I included a rough example here.
You can check out the demo at https://dartpad.dev/087d5cfc1044c8ce742a0c5c7147940f

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    // An example of using Future to return Map<String, dynamic>
    Future<Map<String, dynamic>> _loadData() async {
      Map<String, dynamic> dataFromSharedPref = {};

      // A task that needs some time to load e.g. Getting data from SharedPreference
      // TODO: Change this part to load from your SharedPreference
      await Future.delayed(const Duration(seconds: 1), () {
        dataFromSharedPref = {
          "2022-04-08 15:49:41.864929": "1234567",
          "2022-04-08 15:49:55.392684": "1234567"
        };
      });

      return dataFromSharedPref;
    }

    return FutureBuilder(
        future: _loadData(),
        builder: (context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            // Show loading when there is no data
            return const Center(child: CircularProgressIndicator());
          } else {
            // Build the list when data is received
            var myData = snapshot.data;
            return ListView.builder(
                itemCount: myData.length,
                itemBuilder: (BuildContext context, int index) {
                  String key = myData.keys.elementAt(index);
                  return ListTile(
                    title: Text('Key: $key'),
                    subtitle: Text('Value: ${myData[key]}'),
                  );
                });
          }
        });
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文