获取特定值而不是所有数据

Get a Speicific Value instead of all data

    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));
        });
      }
    
    
    }

这个回复是:

{
  "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
  }
}

我希望它只打印总金额以将其存储在字符串中

响应是嵌套映射

所以你可以得到 total_amount 作为

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

注意:如果您可以控制 api,请不要在属性中使用空格,即 Total Amount => total_amount

确保在获取数据之前验证数据,因为

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