Skip to content

Using Container and/or BoxDecoration color property error is confusing  #36161

@sjghose

Description

@sjghose

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),
        ),
      ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: error messageError messages from the Flutter frameworka: qualityA truly polished experienceframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions