Home > other >  Get a Speicific Value instead of all data
Get a Speicific Value instead of all data

Time:06-03

    import 'package:tagyourtaxi_driver/models/first_token.dart';
    import 'package:tagyourtaxi_driver/modules/payment/cubit/states.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    import 'package:tagyourtaxi_driver/pages/NavigatorPages/walletpage.dart';
    import 'package:tagyourtaxi_driver/pages/login/get_started.dart';
    import 'package:tagyourtaxi_driver/shared/components/constants.dart';
    import 'package:tagyourtaxi_driver/xpay/xpaymodels/prepare_amount.dart';
    import 'package:tagyourtaxi_driver/xpay/xpayshared/xpayconstants.dart';
    import 'package:tagyourtaxi_driver/xpay/xpaycubit/xpaystates.dart';
    import 'package:tagyourtaxi_driver/xpay/xpaymodels/xpay_payment.dart';
    
    import '../../../functions/functions.dart';
    import '../../shared/network/dio.dart';
    
    class XpayCubit extends Cubit<XpayStates>{
      XpayCubit(): super(XpayPaymentInitialState());
    
      static XpayCubit get(context)=> BlocProvider.of(context);
    
    
      PrepareAmount? prepareAmount;
    
      Future getPrepareAmount(String addMoney,String CommunityId) async {
        DioHelperPayment.postData(
            url: 'payments/prepare-amount/',
            data: {
              'community_id': CommunityId,
              'amount': addMoney.toString()
            }).then((value) {
          // TotalAmount=TotalAmount.fromJson(value.data);
          // TotalAmount=firstToken!.token.toString();
          TotalAmount = value.data.toString();
          print('Total Amount: ${TotalAmount}');
          emit(XpayPaymentSuccessState());
        })
            .catchError((error){
          emit(XpayPaymentErrorState(error));
        });
      }
    
    
    }

This response is :

{
  "Total Amount": {
    "status": { "code": 200, "message": "success", "errors": [] },
    "data": {
      "total_amount": 500.0,
      "total_amount_currency": "EGP",
      "KIOSK": { "total_amount": 500.0, "total_amount_currency": "EGP" }
    },
    "count": null,
    "next": null,
    "previous": null
  }
}

and i want it only to print total amount to store it in string

CodePudding user response:

the response is the nested map

so you can get the total_amount as

final _data = value.data;
TotalAmount = _data['Total Amount']['data']['total_amount'];

NOTE: if you have control over api, do not use spaces in attributes i.e. Total Amount => total_amount

be sure to validate the data before fetching that as

if(value.data !=null || value.data.status.code == 200){
    final _data = value.data;
    TotalAmount = _data['Total Amount']['data']['total_amount'];
}
  • Related