Error in converting json string as a json objects

Viewed 68

I am using Completer to listen onstatechanged and everything is working fine i get a json response but what i really want to do is changing the json string to json object using model class by calling MediaData.fromJson(data).. but i always get error...

Here is what i had tried so far

class mainController{
  Future<MediaData> getdata() async{
    final completer = Completer<MediaData>();

    final flutterWebViewPlugin = FlutterWebviewPlugin();

    flutterWebViewPlugin.launch('https://www.instagram.com/picsart/?__a=1', hidden: true, userAgent: kAndroidUserAgent,);


    flutterWebViewPlugin.onStateChanged.listen((viewState) async {
      if (viewState.type == WebViewState.finishLoad) {
        final data = await flutterWebViewPlugin.evalJavascript("document.documentElement.outerText");

       
        completer.complete(MediaData.fromJson(json.decode(data)));


        flutterWebViewPlugin.close();
      }
    });

    return completer.future;
  }
}

Error -

 [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
E/flutter (20186): #0      mainController.getdata.<anonymous closure> (package:igsavers/controller/mainController.dart:20:52)
E/flutter (20186): <asynchronous suspension>
E/flutter (20186): 

I posted the picture of the error in compiler which i get - Error Image

-- Here is my model MediaData rawcode...

1 Answers

The error seems to be in the line 20 enter image description here

Instead of using MediaData.fromJson(json.decode(data)), try MediaData.fromJson(data).

The json.decode must receive an String, but data is currently a Map<String, dynamic>. Then, the usage of this functions is unnecessary.

Related