Repository navigation
Using Container and/or BoxDecoration color property error is confusing #36161
Copy link
Copy link
Closed
Labels
a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworka: qualityA truly polished experienceA truly polished experienceframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.
Description
If color: is specified as Container property and color: is used with BoxDecoration it gives an error which is well and good, except error is at runtime not caught by IDE or compiler.
Also the error should say 'Cannot provide both a color and a decoration.The color argument is just a shorthand for "decoration: new BoxDecoration(color: color). If decoration is used color MUST ONLY be specified with decoration.
In the attached examples error is thrown at runtime
----error example 1---
body: Container(
width: 50.0,
height: 50.0,
color: Colors.blue, //error as well not quite expected
decoration: BoxDecoration(
// color: Colors.red,
border: Border.all(
color: Colors.green, width: 5.0, style: BorderStyle.solid),
),
),
);
----- error example 2---
body: Container(
width: 50.0,
height: 50.0,
color: Colors.blue, //expected error
decoration: BoxDecoration(
color: Colors.red,
border: Border.all(
color: Colors.green, width: 5.0, style: BorderStyle.solid),
),
),
);
------Error Message---
I/flutter (20774): The following assertion was thrown building HomePage(dirty, state: _HomePageState#a88dc):
I/flutter (20774): Cannot provide both a color and a decoration
I/flutter (20774): The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".
I/flutter (20774): 'package:flutter/src/widgets/container.dart':
I/flutter (20774): Failed assertion: line 271 pos 15: 'color == null || decoration == null'
I/flutter (20774):
I/flutter (20774): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (20774): more information in this error message to help you determine and fix the underlying cause.
I/flutter (20774): In either case, please report this assertion by filing a bug on GitHub:
------------------Example----
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new HomePage(),
));
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: 50.0,
height: 50.0,
color: Colors.blue,
decoration: BoxDecoration(
// color: Colors.red,
border: Border.all(
color: Colors.green, width: 5.0, style: BorderStyle.solid),
),
),
);
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
a: error messageError messages from the Flutter frameworkError messages from the Flutter frameworka: qualityA truly polished experienceA truly polished experienceframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.
Activity
Piinks commentedon Jun 27, 2020
I believe this is fixed. Running all of the code snippets above produces a much clearer error message now:

github-actions commentedon Aug 19, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.