I was following udacity course on flutter and getting error "the constructor being called isn't a const constructor" on the following line
const _rowHeight = 100.0;
const _cornerRadius = BorderRadius.circular(_rowHeight / 2);// error is on this line
lib\main. dart prefer_const_literals_to_create_immutables • 1 fix 1 fix made in 1 file. After this, your all warning message will disappear. In this way, you can ignore the 'Prefer const with constant constructors' warning in specific as well as in all files in Flutter/Dart.
A const constructor is an optimization! The compiler makes the object immutable, allocating the same portion of memory for all Text('Hi!') objects. But not Text(Math. random()) though, as its value can't be determined at compile time!
There are several ways to remove this prefer const with constant constructors warning. You can add the ignore line comment at the top of the file. if you want to remove the warning from the file. // ignore_for_file: prefer_const_constructors import 'dart:async'; import 'package:flutter/material.
It is recommended to use const constructors whenever possible when creating Flutter widgets. The reason is the performance increase, since Flutter can save some calculations by understanding that it can reuse that widget from a previous redraw in the current one, since it is a constant value.
This may simply be a bug in border_radius.dart
.
BorderRadius.circular
is defined as (note that the circular
named constructor isn't const
):
/// Creates a border radius where all radii are [Radius.circular(radius)].
BorderRadius.circular(double radius) : this.all(
new Radius.circular(radius),
);
when it seems it could be defined as (though this may break other things):
/// Creates a border radius where all radii are [Radius.circular(radius)].
const BorderRadius.circular(double radius) : this.all(
const Radius.circular(radius),
);
There's a workaround. Change your code to:
const _rowHeight = 100.0;
const _cornerRadius = BorderRadius.all(Radius.circular(_rowHeight / 2));
Equally, you could remove the const: var _cornerRadius =
or BorderRadius cornerRadius =
.
This might be because the widget wrapping _cornerRadius
is a const
. Try removing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With