skip to Main Content

I need to display text and a double in a Text widget but I am getting an error. This should be a simple thing to do.

Here is the code:

double commission = 0.0;

const Text('Commission: ${commission}'),

Here is the error

A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor.
Try using a subtype, or removing the keyword 'const'.dartconst_constructor_param_type_mismatch
Unnecessary braces in a string interpolation.
Try removing the braces.dartunnecessary_brace_in_string_interps
Invalid constant value.dart(invalid_constant)

Nothing I try works. I initialized the variable but it still doesn’t work.

This should be easy but how do I fix it?
Thanks

2

Answers


  1. Remove const because You are trying to embed dynamic value in your widget such that its content can change dynamically where you can’t use const.

    Login or Signup to reply.
  2. Unable to add ‘const’ with dynamic value.

    class MyHomePage extends StatelessWidget {
      final String title;
      const MyHomePage({super.key, required this.title});
    
      @override
      Widget build(BuildContext context) {
        double commission = 0.0;
        return Scaffold(
          body: Center(
            child: Text('Commission : ${commission}'),
          ),
          appBar: AppBar(
            // The title text which will be shown on the action bar
            title: Text(title),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search