這是我的代碼。
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);
}
}
它給出了以下錯誤:
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
我該如何解決這個問題?
uj5u.com熱心網友回復:
在上面代碼的第 8 行,變數swatch被定義為 a Map,默認情況下是 type dynamic,所以完整的型別swatch被創建為Map<dynamic, dynamic>,但是這個變數的值被分配給了Map<int,Color>this的值,這就是為什么你會得到一個錯誤。
要修復此更改:
Map swatch = <int, Color>{};
到
final swatch = <int, Color>{};
dart 中的一個建議是省略區域變數的型別,因為
通常,區域變數的型別很容易推斷,因此不需要對其進行注釋。
有關此建議的更多資訊,請訪問dart 檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339522.html
