hive 配置单元数据未更新

roejwanj  于 2023-10-18  发布在  Hive
关注(0)|答案(1)|浏览(154)

这是我的函数来初始化Hive,并把一些虚拟数据里面,如果它是空的,但数据没有得到显示,我弄清楚的是,它甚至没有得到调用热重启或重新加载,甚至没有当我关闭应用程序重建它,唯一的时间,当我可以看到dummyDietPlan的更新值是当我卸载应用程序并再次构建,为什么_initializeHive()在应用的生命周期中只被调用一次?除了这个问题,其他一切工作正常与Hive,但只是不能弄清楚为什么这个问题是来了

void initState() {
    super.initState();
    _initializeHive();
    _checkDietIsViewed();
  }

  Future<void> _initializeHive() async {
    if (!isInitialized) {
      await HiveService.initHive();
      setState(() {
        isInitialized = true;
      });
    }
  }

  Future<void> _initializeDummyData() async {
    final dietPlans = await HiveService.retrieveDietPlan();
    if (dietPlans.isEmpty) {
      final dummyDietPlan = DietPlan( ... // dummy diet plan inserted
      await HiveService.UpdateDietPlan(dummyDietPlan);
      await HiveService.printDietPlans();
    }
  }
  
   Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<DietPlan>>(
        future: HiveService.retrieveDietPlan(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
          } else if (snapshot.hasError) {
          } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
            // Call _initializeHiveAndData() here to handle empty data
           _initializeHive();
            return Text('No diet plans available.');
          } else {
            final dietPlans = snapshot.data!;
            return ListView.builder(
          }
        },
      ),
    );
  }
}

this is before reinstallation , just a hot restart
This is after reinstallation of the app
我期待着,当我改变值的虚拟饮食计划,它会反映在我的饮食页面,但它没有

lo8azlld

lo8azlld1#

你遇到这个问题是因为你在Build方法中调用FutureBuilder中的_initializeHive方法,
因此,您必须将_initializeHive和_initializeDummyData移动到initState方法中。
在initState方法中,在Hive初始化后调用_initializeDummyData,
以下是正确代码-

class YourPage extends StatefulWidget {
  @override
  _YourPageState createState() => _YourPageState();
}

class _YourPageState extends State<YourPage> {
  bool isInitialized = false;

  @override
  void initState() {
    super.initState();
    _initializeHiveAndData();
    _checkDietIsViewed();
  }

  Future<void> _initializeHiveAndData() async {
    if (!isInitialized) {
      await HiveService.initHive();
      await _initializeDummyData();
      setState(() {
        isInitialized = true;
      });
    }
  }

  Future<void> _initializeDummyData() async {
    final dietPlans = await HiveService.retrieveDietPlan();
    if (dietPlans.isEmpty) {
      final dummyDietPlan = DietPlan(...); // dummy diet plan inserted
      await HiveService.UpdateDietPlan(dummyDietPlan);
      await HiveService.printDietPlans();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<DietPlan>>(
        future: HiveService.retrieveDietPlan(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          } else if (snapshot.hasError) {
            return Text('Error: ${snapshot.error}');
          } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
            return Text('No diet plans available.');
          } else {
            final dietPlans = snapshot.data!;
            return ListView.builder(
              itemCount: dietPlans.length,
              itemBuilder: (context, index) {
                // Build your list item here
              },
            );
          }
        },
      ),
    );
  }
}

相关问题