Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'String' is not a subtype of type 'int' of 'index'

My App can authenticate successfully when debugging against the Android emulator, but if I try to authenticate using debugging against the physical device (with same OS version), an error message appears after more than one minute waiting:

Exception has occurred.

_TypeError (type 'String' is not a subtype of type 'int' of 'index')

The error message points to the following code:

        if (responseJson[AuthUtils.authTokenKey] != null) {
          AuthUtils.insertDetails(
              userNameController.text, passwordController.text, responseJson);
...
        } else {
...
        };

And in the DEBUG CONSOLE I get the following:

I/flutter ( 9531): Auth: SocketException: OS Error: Connection timed out, errno = 110, address = example.com, port = 38975 V/InputMethodManager( 9531): Starting input: tba=android.view.inputmethod.EditorInfo@4aece4e nm : example.com.todoapp ic=null

Here is the screenshot: enter image description here

What am I doing wrong here ?

like image 986
Sami-L Avatar asked Nov 21 '18 16:11

Sami-L


3 Answers

For my case responseJson was of dynamic data type what i did was to cast it to string.

change this

responseJson[AuthUtils.authTokenKey]

to this

String jsonsDataString = responseJson.toString();
final jsonData = jsonDecode(jsonsDataString);

//then you can get your values from the map
if(jsonData[AuthUtils.authTokenKey] != null){
...
}
like image 180
kenn Avatar answered Nov 01 '22 11:11

kenn


I believe that in the line:

responseJson[AuthUtils.authTokenKey]

You're referencing an item in the list responseJson by the index AuthUtils.authTokenKey

And since AuthUtils.authTokenKey is a String, you cannot use it as in index of an array.

So you need first to get the index of AuthUtils.authTokenKey then use it to reference the item:

responseJson[responseJson.indexOf(AuthUtils.authTokenKey)]
like image 20
Mina Wissa Avatar answered Nov 01 '22 09:11

Mina Wissa


This worked for me. I was doing this in flutter.

  http.Response S=await 
  http.get("Your link here");
  String responseBody = S.body;
  dynamic jsonObject = jsonDecode(jsonDecode(S.body));
  print(jsonObject[0]["product_id"]);
  setState(() {
    data=jsonObject[0]["product_id"];
  });
like image 6
Abdullah Arshad Avatar answered Nov 01 '22 09:11

Abdullah Arshad