Search code examples
flutterlocalizationflutter-getx

flutter - how to access map string entries key


i am using getx localization. And my app has a list view inside Text widget.

I try to write in Text widget every language 'name_1' value. But i can not access them.

My translation map below:

  Map<String, Map<String, String>> get keys => {
    'tr_TR': {
      'name_1': 'Dünya',
      'short_desc_1': 'Dünyadan',
          
    },
    'en_US': {
      'name_1': 'world',
      'short_desc_1': 'from world'
      },
    'de_DE': {
      'name_1': 'world',
      'short_desc_1': 'from world'
      },
    'fr_FR': {
      'name_1': 'world',
      'short_desc_1': 'from world'}
      };

I get value below

TranslateStrings().keys[locale]!['name_' + '2']!.tr

But i need to get this with index value.

TranslateStrings().keys[locale]!['name_' + index.tostring()]!.tr

If i write index instead of '2' i am getting error.

_CastError (Null check operator used on a null value)

SOLVED

TranslateStrings().keys[locale]!['name_' + '${index + 1}']!.tr

Solution

  • here some magic trick for you

      keys.forEach(
        (k, v) {
          print(k); //k for keys like  'tr_TR'
          print(v); //v for the map after the key then access the inner value with v['name_1']
        },
      );