Christian Findlay
Christian Findlay

Reputation: 7672

The argument type 'String' can't be assigned to the parameter type 'String'

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?

enter image description here

Upvotes: 1

Views: 729

Answers (3)

Tolga Kartal
Tolga Kartal

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

Christian Findlay
Christian Findlay

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

Christopher Moore
Christopher Moore

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

Related Questions