Flutter 从 GetStorage 列表中获取数据值

Flutter get Data Value from GetStorage List

我想从 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)

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

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

将此添加到您的 PriceModel2 class.

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

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

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

我还建议,无论您使用哪个存储库,都保留所有与存储相关的功能,包括 boxes 在这种情况下,在它自己的 class 中,存储和 returns 任何你需要的数据。这样,如果您需要升级到 HiveObjectBox 等...您可以在一个 class 中完成,而不是在整个应用程序中完成。

示例如下所示。

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 列表的方式。