Reputation: 401
i got an error while using Colors.black property from the material.dart library.
Error : A value of type 'Color' can't be assigned to a variable of type 'String'. Try changing the type of the variable, or casting the right-hand type to 'String'.
import 'package:flutter/material.dart';
class Category {
final String id;
final String title;
final String color;
const Category({
@required this.id,
@required this.title,
this.color = Colors.black,
});
}
any help will be appreciated
Upvotes: 1
Views: 9472
Reputation: 3084
You're trying to assign a type Color to a Variable of type String, change:
final String color;
for:
final Color color;
Dart is a strongly-typed language, it prohibits you from assigning a value of another type to a variable that had it's type declared.
Both Color
and String
are Objects
in Dart, but have different types.
When you're not sure about the type you'll be passing to a variable, declare it with the keyword var
:
var color;
This way you can assign any type to it. Thou it's not advised, since this way you won't be enjoying the benefits you gain from using a strongly-typed language such as Dart.
Upvotes: 6
Reputation:
you can make color's type
final Color color ; or final int color;
Upvotes: 1
Reputation: 13343
final String color;
Declares a variable called color
of type String
.
this.color = Colors.black
Is assigning an object of type Color
to the String
variable.
Change final String color
to final Color color
.
Upvotes: 2