Reputation: 69
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
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
Reputation: 495
I don't know why you're writing this ["key"]
Do this instead:
serverToken = jsonDecode(data);
Upvotes: 0