theeseus
theeseus

Reputation: 69

Flutter app error Exception has occurred. _TypeError (type 'String' is not a subtype of type 'int' of 'index')

I am getting this exception error when trying to run in flutter ( Exception has occurred. _TypeError (type 'String' is not a subtype of type 'int' of 'index') error is line serverToken = jsonDecode(data)["key"];

 void getFCMServerKey() async {
     final RemoteConfig remoteConfig = await RemoteConfig.instance;
    await remoteConfig.fetch(expiration: const Duration(hours: 5));
    await remoteConfig.activateFetched();
     var data = remoteConfig.getString('FcmServerKey');
     if (data != null) {
       serverToken = jsonDecode(data)["key"];
     }   }

Upvotes: 0

Views: 388

Answers (2)

Ali Bayram
Ali Bayram

Reputation: 7941

You got this is error, because when you convert your json data to Dart class jsonDecode(data), you got a List. Probably you assumed get a Map and tried to access item with key parameter and got an error because you can not access items of List with string keys you should use int index number. Before trying to access item be sure that your data is in format that you want.

Upvotes: 0

Faaatman
Faaatman

Reputation: 495

I don't know why you're writing this ["key"]

Do this instead:

serverToken = jsonDecode(data);

Upvotes: 0

Related Questions