2

I'm getting data from my api as a following JSON object to create chart with the data. I want to covert this DIO response to a Map<String, int> object in my flutter app.

My API response:

{
    "November 20": 1,
    "October 20": 3,
    "September 20": 1
}

My try of creating a map object:

  Future<bool> fetchChartData() async {
    var response = await CasesApiService().getChartData();
    Map<String, dynamic> data = jsonDecode(response.data);
  }

But it shows the following error:

E/flutter ( 4733): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
E/flutter ( 4733): #0      CasesProvider.fetchChartData
package:bdopsApp/providers/casesProvider.dart:86
E/flutter ( 4733): <asynchronous suspension>
E/flutter ( 4733): #1      _CasesChartViewState.build.<anonymous closure>
package:bdopsApp/…/casesApp/casesChart.dart:31
E/flutter ( 4733): #2      _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:985
E/flutter ( 4733): #3      _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:1101
E/flutter ( 4733): #4      GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:183
E/flutter ( 4733): #5      TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:598
E/flutter ( 4733): #6      BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:287
E/flutter ( 4733): #7      BaseTapGestureRecognizer.handlePrimaryPointer
package:flutter/…/gestures/tap.dart:222
E/flutter ( 4733): #8      PrimaryPointerGestureRecognizer.handleEvent
package:flutter/…/gestures/recognizer.dart:476
E/flutter ( 4733): #9      PointerRouter._dispatch
package:flutter/…/gestures/pointer_router.dart:77
E/flutter ( 4733): #10     PointerRouter._dispatchEventToRoutes.<anonymous closure>
package:flutter/…/gestures/pointer_router.dart:122
E/flutter ( 4733): #11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter ( 4733): #12     PointerRouter._dispatchEventToRoutes
package:flutter/…/gestures/pointer_router.dart:120
E/flutter ( 4733): #13     PointerRouter.route
package:flutter/…/gestures/pointer_router.dart:106
E/flutter ( 4733): #14     GestureBinding.handleEvent
package:flutter/…/gestures/binding.dart:368
E/flutter ( 4733): #15     GestureBinding.dispatchEvent
package:flutter/…/gestures/binding.dart:348
E/flutter ( 4733): #16     RendererBinding.dispatchEvent
package:flutter/…/rendering/binding.dart:268
E/flutter ( 4733): #17     GestureBinding._handlePointerEventImmediately
package:flutter/…/gestures/binding.dart:303
E/flutter ( 4733): #18     GestureBinding.handlePointerEvent
package:flutter/…/gestures/binding.dart:267
E/flutter ( 4733): #19     GestureBinding._flushPointerEventQueue
package:flutter/…/gestures/binding.dart:225
E/flutter ( 4733): #20     GestureBinding._handlePointerDataPacket
package:flutter/…/gestures/binding.dart:208
E/flutter ( 4733): #21     _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter ( 4733): #22     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 4733): #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 4733): #24     _invoke1 (dart:ui/hooks.dart:265:10)
E/flutter ( 4733): #25     _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)

N.B: I'm doing my final year project. Therefore, the process I'm following might not be what is used in real world project.

2
  • Are you sure that the code snippet you have mentioned above is throwing that error ? . Commented Oct 22, 2020 at 6:42
  • If yes then maybe try to take the decoded value as Map<dynamic,dynamic> or just a var ....... so first try : Map<dynamic,dynamic> data = jsonDecode(...) and if this too throws an error, try var data = jsonDecode(.....) Commented Oct 22, 2020 at 6:44

1 Answer 1

1

If you run the following example in the dartPad you will get the result that you are looking for:

import 'dart:convert';

void main() {
  
  var jsonString= "{\"November 20\": \"1\",\"October 20\": \"2\",\"September 20\": \"3\"}";
  Map<String, dynamic> data = jsonDecode(jsonString);
  
  print(data);
}

I think you need to check the data that you are getting it from the response.data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.