Reputation: 7672
I'm getting the error "The argument type 'String' can't be assigned to the parameter type 'String'" in the code below:
import 'dart:async';
abstract class SettingsBase {
Future<String> getSetting<String>(String key);
Future saveSetting<String>(String key, String value);
}
class InMemorySetting extends SettingsBase {
final settings = <String, String>{};
@override
Future<String> getSetting<String>(String key) {
if (settings.containsKey(key)) {
return Future.value(settings[key] as String);
}
if (key == "theme") {
return Future.value("light" as String);
}
throw UnimplementedError();
}
@override
Future saveSetting<String>(String key, String value) {
settings.putIfAbsent(key, () => value);
return Future.value(true);
}
}
What have I missed?
Upvotes: 1
Views: 729
Reputation: 633
You are using a generic type of String which overwrites Dart's String type.
Just remove the extra String in
getSetting<String>
Upvotes: 2
Reputation: 7672
It turns out that I added an extra generic type argument named String
to the method. The code below works.
import 'dart:async';
abstract class SettingsBase {
Future<String> getSetting<String>(String key);
Future saveSetting(String key, String value);
}
class InMemorySetting extends SettingsBase {
final settings = <String, String>{};
@override
Future<String> getSetting<String>(String key) {
if (settings.containsKey(key)) {
return Future.value(settings[key] as String);
}
if (key == "theme") {
return Future.value("light" as String);
}
throw UnimplementedError();
}
@override
Future saveSetting(String key, String value) {
settings.putIfAbsent(key, () => value);
return Future.value(true);
}
}
Upvotes: 1
Reputation: 17113
Do some reading about what a generic is.
I'm not sure why, but you're adding a generic to your function prototype in your abstract class with the name of String
. So the types of the key
and value
parameters are of the generic type, just with a placeholder name of String
.
Remove those generics:
import 'dart:async';
abstract class SettingsBase {
Future<String> getSetting(String key);
Future saveSetting(String key, String value);
}
class InMemorySetting extends SettingsBase {
final settings = <String, String>{};
@override
Future<String> getSetting(String key) {
if (settings.containsKey(key)) {
return Future.value(settings[key] as String);
}
if (key == "theme") {
return Future.value("light" as String);
}
throw UnimplementedError();
}
@override
Future saveSetting(String key, String value) {
settings.putIfAbsent(key, () => value);
return Future.value(true);
}
}
Upvotes: 2