【问题标题】:Flutter save data by id using Shared PreferencesFlutter 使用 Shared Preferences 按 id 保存数据
【发布时间】:2021-10-21 09:50:10
【问题描述】:

我有一个 listView,其中有一些产品。我想要每个产品详细信息页面上的评级系统,所以我通过这个答案如何在颤动评级栏中保存用户评级?现在的问题是我在每个产品详细信息页面上都获得了相同的星级评分。现在请向我建议,以在我的共享偏好中保存与产品 ID 相关的星值。

或者任何人有其他解决方案,请与我分享。

评分控制者

class RatingController extends GetxController {
  int currentRating = 0;
  final box = GetStorage();

  late SharedPreferences prefs;

  @override
  void onInit() { // called whenever we initialize the controller
    super.onInit();
    currentRating = box.read('rating') ?? 0; // initializing current rating from storage or 0 if storage is null
  }

  void updateAndStoreRating(int rating) {
    currentRating = rating;
    prefs.setInt('rating', rating); //SharedPreferences way
    update(); // triggers a rebuild of the GetBuilder Widget
  }
  Future<void> initSp() async {
    prefs = await SharedPreferences.getInstance();
    currentRating = prefs.getInt('rating') ?? 0;
  }

  Widget buildRatingStar(int index) {
    if (index < currentRating) {
      return Icon(
        Icons.star,
        color: Colors.yellow,
      );
    } else {
      return Icon(
        Icons.star,
        color: Colors.white,
      );
    }
  }
}

评分小部件

SharedPreferences? _prefs;
 

  final controller = Get.find<RatingController>();

  Widget _buildBody() {
    final stars = List<Widget>.generate(5, (index) {
      return GetBuilder<RatingController>( // rebuilds when update() is called from GetX class
        builder: (controller) => Expanded(
          child: GestureDetector(
            child: controller.buildRatingStar(index),
            onTap: () {
              controller.updateAndStoreRating(index + 1);
              print(index + 1);// +1 because index starts at 0 otherwise the star rating is offset by one
            },
          ),
        ),
      );
    });
    return Row(
      children: [
        Expanded(
          child: Row(
            children: stars,
          ),
        ),
        Expanded(
          child: TextButton(
            onPressed: () {
              controller.updateAndStoreRating(0);
            },
            child: Text(
              "Clear",
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
      ],
    );
  }

这个工作和保存星值,但我在所有列表数据的详细信息页面中都得到了这个值。所以请建议我使用特定的索引来保存。

【问题讨论】:

  • 我会为此推荐 Hive,它允许您创建自定义类型适配器,将其存储在一个盒子中并在整个应用程序中访问它docs.hivedb.dev/#
  • 然后您可以创建一个自定义类来存储它,例如 ProductStars,其中包含产品的最终 int 索引和评级的 int 值以访问简单使用 box.get('Your Product Index Here')

标签: android flutter dart flutter-layout sharedpreferences


【解决方案1】:

GetStorage 无法保存基于Map 的数据类型,在您的情况下最好使用Hive

首先,将 hive 添加到您的依赖项中:

  hive: ^2.0.4
  hive_flutter: ^1.1.0

然后在应用程序的开头初始化它:

await Hive.initFlutter();

并像这样使用它:

var box = await Hive.openBox('myBox');
var person = Person()
  ..name = 'Dave'
  ..age = 22;
box.add(person);
print(box.getAt(0)); // Dave - 22
person.age = 30;
person.save();
print(box.getAt(0)) // Dave - 30

您可以在this link 中查看更多关于使用 hive 的示例。

【讨论】:

    猜你喜欢
    • 2019-02-11
    • 2019-08-27
    • 2019-08-27
    • 2021-03-29
    • 2020-10-07
    • 2021-06-28
    • 2023-03-17
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多