qwea
qwea

Reputation: 525

Flutter 2.0 The argument type 'Color?' can't be assigned to the parameter type 'Color'

After I updated the flutter sdk to >=2.12.0 <3.0.0, there's a weird error saying that The argument type 'Color?' can't be assigned to the parameter type 'Color' when I try to assign the border color to the card widget, what is going on here?

Card(
  shape: RoundedRectangleBorder(
    side: BorderSide(color: Colors.blue[300], width: 2.0),
    borderRadius: BorderRadius.circular(15.0)
  ),
  child: Text('Demo')),

Full code to reproduce the error:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Card(
              shape: RoundedRectangleBorder(
                side: BorderSide(color: Colors.blue[300], width: 2.0),
                borderRadius: BorderRadius.circular(15.0),
              ),
              child: Text('Demo')),
        ),
      ),
    );
  }
}

Upvotes: 28

Views: 35662

Answers (3)

Ashley
Ashley

Reputation: 83

Maybe in the future using the bang operator [!]

color: Colors.blue[300]!;

will provoke some problems, if you ever get an error, it might appear the error of: Null check operator used on a null value so to solve that, you should do:

var color = Colors.blue[300];
color: color as Color

try to avoid the bang operator unless you really know what you're doing, hope it helps to someone!

Upvotes: 3

David C
David C

Reputation: 256

You could alternatively do

color: Colors.blue.shade300

which gives the same result.

See the flutter docs for more info: https://api.flutter.dev/flutter/material/Colors-class.html

Upvotes: 20

AL.Sharie
AL.Sharie

Reputation: 1615

just do this

 color: (Colors.blue[300])!,

it is a feature in dart Null safty for more information please check this link

https://medium.com/flutter/null-safety-flutter-tech-preview-cb5c98aba187

Upvotes: 77

Related Questions