Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

This is my code

Map<String, String> coinValues = {};
bool isWaiting = false;
void getData() async {
  isWaiting = true;
  try {
    var data = await CoinData().getCoinData(selectedCurrency);

    isWaiting = false;

    setState(() {
      coinValues = data;
    });
  } catch (e) {
    print(e);
  }
}
Future<Column> makeCards() async {
  List<CryptoCard> cryptoCards = [];
  for (String crypto in cryptoList) {
    cryptoCards.add(
      CryptoCard(
        cryptoCurrency: crypto,
        selectedCurrency: selectedCurrency,
        value: isWaiting ? '?' : coinValues[crypto],
      ),
    );
  }
  return Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: cryptoCards,
  );
}

I get the following error

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.
          value: isWaiting ? '?' : coinValues[crypto],

I tried adding a null check but the app crash and says "Null check operator used on a null value"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It means that your coinValues[crypto] returns null.

You should check if coinValues[crypto] could be null or not.

Otherwise you can try the following code :

Future<Column> makeCards() async {
  List<CryptoCard> cryptoCards = [];
  for (String crypto in cryptoList) {
    cryptoCards.add(
      CryptoCard(
        cryptoCurrency: crypto,
        selectedCurrency: selectedCurrency,
        value: isWaiting || coinValues[crypto] == null ? '?' : coinValues[crypto]!,
      ),
    );
  }
  return Column(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: cryptoCards,
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading