Flutter - 如何在构建方法中只执行一次函数?

Flutter - How to execute a function only once in build method?

我试图让一个函数只运行(渲染一个小部件)一次。这是我的屏幕的主要构建功能。您可以忽略大部分代码,只看到底部的注释行。

  Widget build(BuildContext context) {
  workoutId = box.get('workoutId');
  
  final arguments = ModalRoute.of(context)!.settings.arguments as Map;
  exerciseName = arguments["exerciseName"];
  exerciseId = arguments["exerciseId"];
  return Scaffold(
    appBar: AppBar(
      leading: IconButton(
          icon: const Icon(
            Icons.arrow_back,
            color: kBlack,
          ),
          onPressed: () {
            Navigator.pop(context);
          }),
      backgroundColor: Colors.transparent,
      title: Text(
        exerciseName,
        style: const TextStyle(
            color: kAppTextColor, fontSize: kAppButtonTextFontSize),
      ),
    ),
    body: Container(

      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: Container(
              height: SizeConfig.screenHeight,
              padding: const EdgeInsets.all(0),
              child: ClipRRect(
                child: BackdropFilter(
                  filter: ImageFilter.blur(sigmaX: 15, sigmaY: 15),
                  child: Container(

                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        _buildInputHeaderRowWidget(),
                        _buildInputRowWidget(),
                        _buildDataTable(),
                        const Padding(
                          padding: EdgeInsets.only(top: 10),
                          child: Divider(
                            height: 10,
                            color: Colors.grey,
                            thickness: 3,
                          ),
                        ),
                        //_buildDataTableForCompletedExercises(),
                        Expanded(
                          child: Container(
                            child: (workoutId != null) 
                                ? _buildDataTableForCompletedExercises() ///THIS NEEDS TO RUN ONLY ONCE
                                : const Text(
                                    'Workout ID is null',
                                    style: TextStyle(
                                        fontSize: kAppButtonTextFontSize),
                                  ),
                          ),
                        ),

你是如何做到这一点的?我需要设置标志还是什么?

好的,所以你想做的基本上是“缓存”你显示的函数的结果,这是我认为你可以做的:

首先声明一个可为空的小部件,您可以随意调用它:

Widget? _completedExercises;

然后:

  1. 将其指定为初始状态的函数:
void initState() {
  _completedExercises = _buildDataTableForCompletedExercises();
  super.initState();
}

然后在构建方法上使用小部件:

...
child: (workoutId != null) 
  ? _completedExercises!
  : const Text(
...

或 2. 使函数本身缓存值:

Widget _buildDataTableForCompletedExercises() {
  if (_completedExercises == null) {
    _completedExercises = Container(); // here put whatever your function used to be.
  }
  return _completedExercises!;
}

想法是函数第一次运行时,它将分配 _completedExercises 的值,然后所有后续运行将 return 之前的值。