2

I am parsing the JSON like from key and values like below code in flutter

 Future<LoginBean> login() async {
    var body = json.encode({"MOB": "1112223330", "KEY": "123456"});

    return http.post(
        Uri.encodeFull(
            "https://MY_Server/Users/login"),
        body: body.toString(),
        headers: {'Content-type': 'application/json'}).then((response) {
      print("Response Status : $response");

      if (response.statusCode == 200) {
         Map userMap = jsonDecode(json);
         var user = new LoginBean.fromJson(userMap);

         debugPrint("here is the response==>>>> $user");
      }
    });
  }

Is there any other way to parse the JSON in Model class as we do in Android, please check the below example.

Gson gson = new Gson();

MY_LOGIN_BEAN loginBean = gson.fromJson(response, MY_LOGIN_BEAN.class);

I have referred this links but not getting the solution, please check below:-
1). Link
2). Link
3). Link

This is my JSON reponse

{"status":1,"message":"Login Successfully","data":{"userid":"101","Authorization":"eyJ1c2VyaWQiOiIxMDEiLCJ0b2tlbiI6IjVjYWRkYzUzMWY4YzAifQ==","roles":"2","firstname":"Ravindra","lastname":"kushwaha","fullname":"Ravindra kushwaha","wallet_amount":"845.00","mobile":"1112223330","email":"[email protected]","chat_id":"","qrcode_image":"https:\/\/dapplepay.consagous.co.in\/uploads\/coupon_qr\/11C1f7ApJM8r.png","redirect_to_verify":"0","notification_status":"0","country_code":"91","Is_Allowed_Transaction":"1","profile_image":"https:\/\/dapplepay.consagous.co.in\/uploads\/user\/1553600479DapplePay1553600454461.png","fingerprint_status":"0"}}

I have tried below lines of code for it, please have a look on it , but I did not get any success on it.

  class LoginBean {
  int status;
  String message;
  Data data;

  LoginBean({
    this.status,
    this.message,
    this.data,
  });


  static Map<String, dynamic> toMap(LoginBean loginBean){
    var map = Map<String, dynamic>();
    map['status'] = loginBean.status;
    map['message'] = loginBean.message;
    map['data'] = Data.toMap(loginBean);
    return map;
  }

  LoginBean.map(dynamic obj) {
    this.status = obj["status"];
    this.message = obj["message"];
    if (obj['data'] != null) {
      this.data = new Data.map(obj['data']);
    }
  }

  factory LoginBean.fromJson(dynamic json) {
    print('here we gooo');
    return LoginBean(
        status: json['status'],
        message: json['message'],
        data: new Data.fromJson(json['data'])
    );
  }

}

class Data {
  String userid;
  String authorization;
  String roles;
  String firstname;
  String lastname;
  String fullname;
  String walletAmount;
  String mobile;
  String email;
  String chatId;
  String qrcodeImage;
  String redirectToVerify;
  String notificationStatus;
  String countryCode;
  String isAllowedTransaction;
  String profileImage;
  String fingerprintStatus;


  Data.map(dynamic json) {
    this.userid = json['userid'];
    this.authorization = json['authorization'];
    this.roles = json['roles'];
    this.firstname = json['firstname'];
    this.lastname = json['lastname'];
    this.fullname = json['fullname'];
    this.walletAmount = json['walletAmount'];
    this.mobile = json['mobile'];
    this.email = json['email'];
    this.chatId = json['chatId'];
    this.qrcodeImage = json['qrcodeImage'];
    this.redirectToVerify = json['redirectToVerify'];
    this.notificationStatus = json['notificationStatus'];
    this.countryCode= json['countryCode'];
    this.isAllowedTransaction = json['isAllowedTransaction'];
    this.profileImage = json['profileImage'];
    this.fingerprintStatus = json['fingerprintStatus'];
  }


  static Map<String, dynamic> toMap(LoginBean loginBean){
    var map = Map<String, dynamic>();
    map['userid'] = loginBean.data.userid;
    map['authorization'] = loginBean.data.authorization;
    map['roles'] = loginBean.data.roles;
    map['firstname'] = loginBean.data.firstname;
    map['lastname'] = loginBean.data.lastname;
    map['fullname'] = loginBean.data.fullname;
    map['walletAmount'] = loginBean.data.walletAmount;
    map['mobile'] = loginBean.data.mobile;
    map['email'] = loginBean.data.email;
    map['chatId'] = loginBean.data.chatId;
    map['qrcodeImage'] = loginBean.data.qrcodeImage;
    map['redirectToVerify'] = loginBean.data.redirectToVerify;
    map['notificationStatus'] = loginBean.data.notificationStatus;
    map['countryCode'] = loginBean.data.countryCode;
    map['isAllowedTransaction'] = loginBean.data.isAllowedTransaction;
    map['profileImage'] = loginBean.data.profileImage;
    map['fingerprintStatus'] = loginBean.data.fingerprintStatus;
    return map;
  }

  factory Data.fromJson(dynamic json) {
    return Data(
      userid: json['userid'],
      authorization: json['authorization'],
      roles: json['roles'],
      firstname: json['firstname'],
      lastname: json['lastname'],
      fullname: json['fullname'],
      walletAmount: json['walletAmount'],
      mobile: json['mobile'],
      email: json['email'],
      chatId: json['chatId'],
      qrcodeImage: json['qrcodeImage'],
      redirectToVerify: json['redirectToVerify'],
      notificationStatus: json['notificationStatus'],
      countryCode: json['countryCode'],
      isAllowedTransaction: json['isAllowedTransaction'],
      profileImage: json['profileImage'],
      fingerprintStatus: json['fingerprintStatus'],

    );
  }

  Data({
    this.userid,
    this.authorization,
    this.roles,
    this.firstname,
    this.lastname,
    this.fullname,
    this.walletAmount,
    this.mobile,
    this.email,
    this.chatId,
    this.qrcodeImage,
    this.redirectToVerify,
    this.notificationStatus,
    this.countryCode,
    this.isAllowedTransaction,
    this.profileImage,
    this.fingerprintStatus,
  });
}

I am getting the below exception from the above code, while i am using the model class to parse the JSON

2019-04-10 18:59:28.553 11121-11153/democom.first_flutter_app E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
    #0      _LoginScreen.login.<anonymous closure> (package:first_flutter_app/onBording/login_screen.dart:139:34)
    #1      _rootRunUnary (dart:async/zone.dart:1132:38)
    #2      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
    #3      _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
    #4      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
    #5      Future._propagateToListeners (dart:async/future_impl.dart:668:32)
    #6      Future._complete (dart:async/future_impl.dart:473:7)
    #7      _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
    #8      _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
    #9      _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:294:13)
    #10     _withClient (package:http/http.dart)
    <asynchronous suspension>
    #11     post (package:http/http.dart:70:3)
    #12     _LoginScreen.login (package:first_flutter_app/onBording/login_screen.dart:129:12)
    <asynchronous suspension>
    #13     _LoginScreen.build.<anonymous closure>.<anonymous closure> (package:first_flutter_app/onBording/login_screen.dart:102:25)
    #14     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
    #15     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
    #16     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
    #17     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
    #18     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
    #19     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:369:9)
    #20     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
    #21     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
    #22     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:214:19)
    #23     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
    #24     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
    #25     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
    #26     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
    #27     _rootRunUnary (dart:async/zone.dart:1136:13)
    #28     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
    #29     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
    #30     _invoke1 (dart:ui/hooks.dart:223:10)
    #31     _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
14
  • see flutter.dev/docs/development/data-and-backend/json Commented Apr 10, 2019 at 12:25
  • Thanks @pskink for input. Let u know soon Commented Apr 10, 2019 at 12:26
  • @pskink Have tried your solution but not getting the solution, i am editing the questions Commented Apr 10, 2019 at 13:07
  • What is the problem you are facing? your userId should be correct. (Although you aren't printing it properly according to what you have pasted.) Commented Apr 10, 2019 at 13:28
  • 1
    Why don't you use @JsonSerializable attribute approach for your deserialization from Flutter documentation ? Commented Apr 10, 2019 at 14:28

1 Answer 1

2

You are decoding json when you should be decoding body received in response like this -

var userMap = jsonDecode(response?.body);
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.