Here is my code.
import 'package:flutter/material.dart';
class WpCreateColor {
final Color wpColor = Color.fromARGB(255, 77, 203, 79);
MaterialColor createMaterialColor() {
List strengths = <double>[.05];
Map swatch = <int, Color>{};
final int r = wpColor.red, g = wpColor.green, b = wpColor.blue;
for (int i = 1; i < 10; i ) {
strengths.add(0.1 * i);
}
strengths.forEach((strength) {
final double ds = 0.5 - strength;
swatch[(strength * 1000).round()] = Color.fromRGBO(
r ((ds < 0 ? r : (255 - r)) * ds).round(),
g ((ds < 0 ? g : (255 - g)) * ds).round(),
b ((ds < 0 ? b : (255 - b)) * ds).round(),
1,
);
});
return MaterialColor(wpColor.value, swatch);
}
}
And it gives the following error:
Map<dynamic, dynamic> swatch
The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter type 'Map<int, Color>'.dartargument_type_not_assignable
How can I solve this?
CodePudding user response:
On line 8 of the code above the variable swatch
is defined as a Map
which by default is type dynamic
, so the full type of swatch
is created as Map<dynamic, dynamic>
, however the value for this variable is assigned to a value of Map<int,Color>
this is why you get an error.
To fix this change:
Map swatch = <int, Color>{};
to
final swatch = <int, Color>{};
A suggestion in dart is to omit types for local variables, because
Usually, the types of local variables can be easily inferred, so it isn’t necessary to annotate them.
Visit the dart docs for more info on this suggestion