I'm trying to implement different languages (english & spanish) in my app, this is what I was thinking:
String selectedLanguage = 'en';
const Map<String, Map<String, String>> languageLabels = {
'en': {
'label1': 'label in English',
},
'es': {
'label1': 'label in Spanish',
},
};
And I was planning to use it as:
static String label1 = languageLabels[selectedLanguage['label1']];
But I keep getting this error
Error: A value of type 'String' can't be assigned to a variable of type 'int'.
static String label1 = languageLabels[selectedLanguage['label1']];
___________________________________________________________^
I read that it was because of null safety, but I just don't know how to solve it.
CodePudding user response:
Try assigning it this way
String? label1 = languageLabels[selectedLanguage]?['label1'];
OR
String label1 = languageLabels[selectedLanguage]?['label1'] ?? '';
OR (I do not suggest it) if you are sure you will never get null
value:
String label1 = languageLabels[selectedLanguage]!['label1']!;