Skip to content

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

Closed

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

Activity

  1. added
    a: error messageError messages from the Flutter framework
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Sep 29, 2019
  2. Piinks commented on Jun 27, 2020

    @Piinks
    Contributor

    I believe this is fixed. Running all of the code snippets above produces a much clearer error message now:
    Screen Shot 2020-06-26 at 6 01 16 PM

  3. github-actions commented on Aug 19, 2021

    @github-actions

    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 -v and a minimal reproduction of the issue.

  4. locked as resolved and limited conversation to collaborators on Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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