25

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 ?

Community
  • 1
  • 1
Sami-L
  • 5,553
  • 18
  • 59
  • 87
  • `responseJson` seems to be inferred as type `List` and expects an integer in the index accessor `responseJson[intIndexhere]`, but `AuthUtils.authTokenKey` is a `String` which is not supported here. That would only be supported if `responseJson` would be a `Map` – Günter Zöchbauer Nov 21 '18 at 16:30

11 Answers11

19

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){
...
}
kenn
  • 1,384
  • 12
  • 19
6

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"];
  });
Abdullah Arshad
  • 156
  • 2
  • 6
  • You have to made Model of that Response String and use that value.. – Sanskardham Gurukul Jan 23 '21 at 11:30
  • if(response.statusCode == 200){ var responseString = response.body; print("responseString is $responseString"); var valueMap = json.decode(responseString); print(valueMap); return WithdrawResponseModel.fromJson(json.decode(valueMap)); }else{ return null; } – Sanskardham Gurukul Jan 23 '21 at 11:30
5

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)]
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • Tried your solution, but instead getting the error message in the screenshot, I got the following: Could not load source 'dart:core/runtime/libstring_patch.dart': . – Sami-L Nov 21 '18 at 16:48
3

'String' is not a subtype of type 'int' of 'index' directly mean you are using String in place of Int here: responseJson[AuthUtils.authTokenKey] takes an int as an index where -> AuthUtils.authTokenKey is a String . *

Print your response.body in the console and check where is your authTokenKey is stored.

* you can get its index directly by using indexOf in your response and use in response as an index to get authTokenKey

akash maurya
  • 607
  • 9
  • 16
3

Got this error when testing a database in Firebase Firestore.

The following assertion was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState>#52d4b): type 'String' is not a subtype of type 'int'

The problem was my document field had a type of string, but it was expecting a number. So, changed from string to number and it worked.

enter image description here

live-love
  • 48,840
  • 22
  • 240
  • 204
3

In my case, I forgot to decode JSON, and that's why I was getting such error.

So don't forget to do like this before parsing it:

final result = json.decode(response.body);

result[AuthUtils.authTokenKey]
SardorbekR
  • 1,388
  • 3
  • 18
  • 33
1

I had an issue very similar to this. It was caused by a conditional assignment to a var that was causing the type to change at run time. Take a look at what NetworkUtil.authencateUser is returning. I bet it's a Future Try changing it to a Future>.

Do you have a different SHA1 key for your emulator? That could be the cause of the differences between the two environments.

Brad
  • 71
  • 2
  • 2
1

Check whether the response is returning an Array of data.

{[{authTokenKey: 'asbc'}]}

In this case, you need to take the [0] element. Then you will get the Map in which you can access indexes with String values.

var authTokenElm = responseJson[0];
var authToken = authTokenElm[AuthUtils.authTokenKey];
thusith.92
  • 306
  • 4
  • 14
1
$json_encode = json_encode($query_array);
$json_encode = str_replace("[", "", $json_encode);
$json_encode = str_replace("]", "", $json_encode);
echo $json_encode;
1

For those who are making API post/put request please send a Map,

final payload = {data: [Array should be sent in this format]}

when you want to send Array of Map<String, dynamic> to the API body.

Alish Giri
  • 1,855
  • 18
  • 21
0

My problem is resolved just by changing the pdo_mysql to nd_pdo_mysql PHP extension. Changes in PHP Extension..

uniqshiva
  • 1
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Muhammad Hussain Oct 18 '22 at 06:29