【问题标题】:Context: Not a constant expression, error on dart上下文:不是常量表达式,飞镖错误
【发布时间】:2022-01-13 19:54:23
【问题描述】:

我正在学习 udemy 课程,但我遇到了这个错误,我不知道它为什么会发生

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  VoidCallback selectHandler;

  Answer(this.selectHandler);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: const RaisedButton(
        color: Colors.blue,
        child: Text('Answer 1'),
        onPressed: selectHandler,
      ),
    );
  }
}

onPressed: selectHandler, 这一行是有问题的行。 我尝试删除随机关键字,但我真的不知道该怎么办。

这是错误日志

lib/answer.dart:15:20: Error: Not a constant expression.
        onPressed: selectHandler,
                   ^^^^^^^^^^^^^
lib/answer.dart:12:20: Error: Constant evaluation error:
      child: const RaisedButton(
                   ^
lib/answer.dart:15:20: Context: Not a constant expression.
        onPressed: selectHandler,

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    您不能将函数放在 const 表达式中,只需从以下位置删除 const 关键字:

    import 'package:flutter/material.dart';
    
    class Answer extends StatelessWidget {
      VoidCallback selectHandler;
    
      Answer(this.selectHandler);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          width: double.infinity,
          child: /* const -> Remove this const keyword */ RaisedButton(
            color: Colors.blue,
            child: Text('Answer 1'),
            onPressed: selectHandler, // This is not a constant because the compiler can't assign the value at the compile time, so we can't use that in a const expression
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      RaisedButton 中删除const 并改为将您的小部件设为const

      class Answer extends StatelessWidget {
        final VoidCallback selectHandler; // Made final
      
        const Answer(this.selectHandler); // const added
      
        @override
        Widget build(BuildContext context) {
          return Container(
            width: double.infinity,
            child: RaisedButton( // const removed
              color: Colors.blue,
              child: Text('Answer 1'),
              onPressed: selectHandler,
            ),
          );
        }
      }
      

      【讨论】:

        【解决方案3】:
        class Answer extends StatelessWidget {
        
         VoidCallback selectHandler; 
         Answer(this.selectHandler);
        
          @override
          Widget build(BuildContext context) {
            return Container(
              width: double.infinity,
              /// removed const from here
              child: RaisedButton( 
                color: Colors.blue,
                child: Text('Answer 1'),
                onPressed: selectHandler,
              ),
            );
          }
        }
        

        【讨论】:

          【解决方案4】:

          onPressed 函数中调用 variable 时,您在 RaisedButton 之前使用了 const。尝试从 RaisedButton 中删除 const 关键字。它应该这样工作。

          import 'package:flutter/material.dart';
          
          class Answer extends StatelessWidget {
            VoidCallback selectHandler;
          
            Answer(this.selectHandler);
          
            @override
            Widget build(BuildContext context) {
              return Container(
                width: double.infinity,
                child: const RaisedButton(
                  color: Colors.blue,
                  child: Text('Answer 1'),
                  onPressed: selectHandler,
                ),
              );
            }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-03-09
            • 1970-01-01
            • 2021-12-03
            • 1970-01-01
            • 1970-01-01
            • 2021-09-09
            • 1970-01-01
            相关资源
            最近更新 更多