【问题标题】:Flutter get Data Value from GetStorage ListFlutter 从 GetStorage 列表中获取数据值
【发布时间】:2021-10-01 20:49:40
【问题描述】:

我想从 GetStorage() 获取数据,

我有一个商店列表,我可以将它用作静态页面,这样我就可以将我的列表保存在 GetStorage 中,如果我转到另一个也会显示的页面。就像一个 SharedPreferences。如何显示来自 GetStorage List Value 的数据,例如

Text(basket[name]),将只显示名称, Text(basket[price]),将只显示价格,

var basketsd = GetStorage("totalList");

我的商店产品模型,

class PriceModel2 {
  final String name;
  final double price;
  PriceModel2({
    this.name,
    this.price,
  });
}

它也不起作用。

Text(controller.basketsd.read(["name"])

 Container(
            height: Get.size.height * 0.3,
            child: Obx(() => ListView.builder(
                itemCount: controller.basket.length,
                itemBuilder: (BuildContext context, int index) {
                  return Column(
                    children: [
                      SizedBox(
                        height: 20,
                      ),
                      Container(
                        width: Get.size.width,
                        child: ElevatedButton(
                            onPressed: () {
                              controller.basket
                                  .remove(controller.basket[index]);
                            },
                            child: (Text(controller.basketsd.read(["name"]) +
                                " " +
                                controller.basket[index].price.toString() +
                                " €"))),
                      ),
                      SizedBox(
                        height: 20,
                      ),
                    ],
                  );
                })),
          ),

代码显示如下, (https://prnt.sc/1fs6mbf)

【问题讨论】:

    标签: android list flutter dart flutter-getx


    【解决方案1】:

    这里有几个问题。至少从您共享的内容来看,您永远不会真正正确地存储您的 PriceModel2 对象。如果您尝试存储一个随机的自定义对象,GetStorage 将不知道如何处理它。 (SharedPreferences 在相同的情况下也不会)。

    因此,对于初学者,您可以实现 toMap 函数,将其转换为 GetStorage 可以读取的地图。

    将此添加到您的 PriceModel2 课程中。

      Map<String, dynamic> toMap() {
        return {
          'name': name,
          'price': price,
        };
      }
    

    然后你在同一个类中添加一个命名构造函数,这样你就可以从你存储的Map创建你的对象。

     PriceModel2.fromMap(Map map)
          : name = map['name'],
            price = map['price'];
    

    我还建议,无论您使用哪个存储库,都将所有与存储相关的功能(包括本例中的 boxes)保留在其自己的类中,以存储和返回您需要的任何数据。这样,如果您需要升级到 HiveObjectBox 等……您可以在一个班级中完成,而不是在整个应用程序中完成。

    示例如下所示。

    class Database extends GetxController {
      final box = GetStorage();
    
      Future<void> initStorage() async {
        await GetStorage.init();
      }
    
      void storePriceModel(PriceModel2 model) {
        box.write('model', model.toMap());
      }
    
      PriceModel2 restoreModel() {
        final map = box.read('model') ?? {};
        return PriceModel2.fromMap(map);
      }
    }
    
    

    然后您将在 main 中初始化您的控制器和存储。

    void main() async {
      await Get.put(Database()).initStorage();
      runApp(MyApp());
    }
    

    存储模型的示例如下所示。

         ElevatedButton(
                  onPressed: () {
                    final model = PriceModel2(name: 'test name', price: 23.0);
                    Get.find<Database>().storePriceModel(model);
                  },
                  child: Text('Store Model'),
                ),
    

    并且可以像这样在 UI 中显示存储数据。

    Text(Get.find<Database>().restoreModel().name)
    
    

    您不需要 Obx 来显示存储的数据,因为它不是基于可观察流的变量。

    至于显示产品列表,您做同样的事情,但存储地图列表而不是单个地图。

    如果您需要这方面的帮助,请分享您尝试添加和存储到PriceModel2 列表的方式。

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2018-11-29
      • 2021-09-05
      • 2017-02-08
      • 2019-07-27
      • 2020-11-05
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多