title: How to turn part of the response from Dio into a variable in Flutter/Dart?
body: <p>Using Dio, I make a specific request to the site:</p>
  <pre class="lang-dart prettyprint-override"><code>final response = await Dio().get(
  …
  print(response); //Just to see my response
  </code></pre>
  <p>Here is the response from the console:</p>
  <pre class="lang-dart prettyprint-override"><code>I/flutter ( 4594): {&quot;records&quot;:[{&quot;id&quot;:&quot;recrNhzWnPdiSFaCs&quot;,&quot;createdTime&quot;:&quot;2022-07-28T15:45:23.000Z&quot;,&quot;fields&quot;:{&quot;Name&quot;:&quot;Cactus &quot;,&quot;Data&quot;:&quot;11.5&quot;,&quot;App&quot;:&quot;The Best App&quot;,&quot;Current&quot;:&quot;98789fg43&quot;}}]}
  </code></pre>
  <p>I am satisfied with the data. They are correct. For further work, I only need the recrNhzWnPdiSFaCs value. I want to pass it to a variable that will be used in another part of the code. How to do it?</p>
  <p><strong>Edit1.</strong>
  Using <code>var decodedString = json.decode(response.data.toString());</code> does not cause any errors in the code. But on startup, the following appears in the console:</p>
  <pre class="lang-dart prettyprint-override"><code>I/flutter ( 4594): {&quot;records&quot;:[{&quot;id&quot;:&quot;recrNhzWnPdiSFaCs&quot;,&quot;createdTime&quot;:&quot;2022-07-28T15:45:23.000Z&quot;,&quot;fields&quot;:{&quot;Name&quot;:&quot;Cactus &quot;,&quot;First&quot;:&quot;11.5&quot;,&quot;App&quot;:&quot;The Best App&quot;,&quot;Current&quot;:&quot;98789fg43&quot;}}]}
  E/flutter ( 4594): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 2)
  E/flutter(4594): {records: [{id: recrNhzWnPdiSFaCs, createdTime: 2022-07-28T15:45:23.000Z, f...
  E/flutter ( 4594): ^
  E/flutter ( 4594):
  E/flutter ( 4594): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
  E/flutter ( 4594): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
  E/flutter ( 4594): #2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
  E/flutter ( 4594): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
  E/flutter ( 4594): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
  E/flutter(4594): #5 _MainWidgetState.airtableFind(package:example/main.dart:214:32)
  E/flutter ( 4594): &lt;asynchronous suspension&gt;
  E/flutter ( 4594):
  </code></pre>
  <p>And here is the whole code in case I made a mistake somewhere:</p>
  <pre class="lang-dart prettyprint-override"><code>//Airtable (find a record)
    void airtableFind() async {
      try {
        final response = await Dio().get(
          'https://api.airtable.com/v0/'+projectBase+'/'+recordName,
          queryParameters: {
            // 'filterByFormula': 'SEARCH(&quot;Cactus&quot;,{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
            'filterByFormula': 'SEARCH('+'&quot;'+testName+'&quot;'+',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
            // 'filterByFormula': 'SEARCH('+testName+',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
          },
          options: Options(
            contentType: 'Application/json',
            headers: {
              'Authorization': 'Bearer'+' '+apiKey,
              'Accept': 'Application/json',
            },
          ),
        );
  
        // TODO: Whatever you want to do with the response. A good practice is to transform it into models and than work with them
        print(response);
        // String fromString = response;
  
        // String response1 = '{&quot;records&quot;:[{&quot;id&quot;:&quot;recrNhzWnPdiSFaCs&quot;,&quot;createdTime&quot;:&quot;2022-07-28T15:45:23.000Z&quot;,&quot;fields&quot;:{&quot;Name&quot;:&quot;Cactus &quot;,&quot;Data&quot;:&quot;11.5&quot;,&quot;App&quot;:&quot;The Best App&quot;,&quot;Current&quot;:&quot;98789fg43&quot;}}]}';
        var decodedString = json.decode(response.data.toString());
        // final decodedJSON = json.decode(response.body);
        // final decodedJSON = json.decode(utf8.decode(response.bodyBytes));
  
        print(decodedString['records'][0]['id']);
        String id = decodedString['records'][0]['id'];
  
  
      } on DioError catch (e) {
        // TODO: Error handling
        if (e.response != null) {
          // print(e.response.data);
          print(e);
        } else {
          // print(e.request);
          print(e.message);
        }
      }
    }
  </code></pre>
  <p><strong>Edit2.</strong> When I use <code>final decodedString = json.decode(response.data);</code> I see the following errors in the console:</p>
  <pre><code>I/flutter( 4289): {&quot;records&quot;:[{&quot;id&quot;:&quot;recrNhzWnPdiSFaCs&quot;,&quot;createdTime&quot;:&quot;2022-07-28T15:45:23.000Z&quot;,&quot;fields&quot;:{&quot;Name&quot;:&quot;Cactus &quot;,&quot;First&quot;:&quot;11.5&quot;,&quot;App&quot;:&quot;The Best App&quot;,&quot;Current&quot;:&quot;98789fg43&quot;}}]}
  E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap&lt;String, dynamic&gt;' is not a subtype of type 'String'
  E/flutter(4289): #0 _MainWidgetState.airtableFind(package:example/main.dart:217:50)
  E/flutter ( 4289): &lt;asynchronous suspension&gt;
  </code></pre>
  <p>When I use <code>var decodedString = json.decode(response.data.toString());</code> the following errors appear in the console:</p>
  <pre><code>I/flutter( 4289): {&quot;records&quot;:[{&quot;id&quot;:&quot;recrNhzWnPdiSFaCs&quot;,&quot;createdTime&quot;:&quot;2022-07-28T15:45:23.000Z&quot;,&quot;fields&quot;:{&quot;Name&quot;:&quot;Cactus &quot;,&quot;First&quot;:&quot;11.5&quot;,&quot;App&quot;:&quot;The Best App&quot;,&quot;Current&quot;:&quot;98789fg43&quot;}}]}
  E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 2)
  E/flutter(4289): {records: [{id: recrNhzWnPdiSFaCs, createdTime: 2022-07-28T15:45:23.000Z, f...
  E/flutter ( 4289): ^
  E/flutter ( 4289):
  E/flutter ( 4289): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
  E/flutter ( 4289): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
  E/flutter ( 4289): #2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
  E/flutter ( 4289): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
  E/flutter ( 4289): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
  E/flutter(4289): #5 _MainWidgetState.airtableFind(package:example/main.dart:217:32)
  E/flutter ( 4289): &lt;asynchronous suspension&gt;
  </code></pre>