flutter 在整个API响应字符串中,我只想得到特定的单词,该怎么做?

h79rfbju  于 2023-02-16  发布在  Flutter
关注(0)|答案(3)|浏览(140)

下面是响应,我正在获取图像路径。该文件可以是pdf或jpeg或任何其他格式。对于图像,我显示它在对话框中,对于pdf,我必须将其下载到用户的device.For,首先我必须检查文件是pdf或不是,我想从整个路径中获得像pdf或jpeg文件格式的扩展名(如0th和1st所示)。如何使用拆分字符串我尝试了几种方法,但没有得到结果。请指导我。

0:"https://xuriti-prod-kyc.s3.ap-south-1.amazonaws.com/27AAACB2100P1ZX/1675230131954-download-%281%29.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAXALYDECMNE2HOZS7%2F20230216%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20230216T063528Z&X-Amz-Expires=900&X-Amz-Signature=59204e81b56cd325de1531a1e6c6a67c5030c76c0516b4719e40986e1ef1d917&X-Amz-SignedHeaders=host"

1:"https://xuriti-prod-kyc.s3.ap-south-1.amazonaws.com/27AAACB2100P1ZX/1675230131972-download.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAXALYDECMNE2HOZS7%2F20230216%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20230216T063528Z&X-Amz-Expires=900&X-Amz-Signature=867311e96282f231406f46d5f2a5d11befe985b0b3a0ab161f69dfb3465b50a9&X-Amz-SignedHeaders=host"

下面是我的代码,我试图拆分并获得文件格式:

List imgfiles = [];
  @override
  void initState() {
    init();
    super.initState();
  }

  Future init() async {
    dynamic companyId = getIt<SharedPreferences>().getString('companyId');

    //final docs = DioClient().KycDetails(companyId);
    dynamic responseData = await getIt<DioClient>().KycDetails(companyId);
    final details = responseData['data'];
    Aadhar Docdetails = Aadhar.fromJson(details['aadhar']);
    setState(() {
      List<String> imgfiles = Docdetails.files;
      this.imgfiles = imgfiles;
    });
  }
  Padding(
                padding: EdgeInsets.only(
                  left: w1p * 3,
                  right: w1p * 6,
                ),
                child: SizedBox(
                  width: maxWidth,
                  height: 50,
                  child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: imgfiles.length,
                    itemBuilder: (context, index) {
                      String doc = imgfiles[index];
                    print('the whole filepath  >>>>>>>>$doc');

                    String doc1 = doc.split('jpeg').toString();
                    print('doc1.>>>>>>>>$doc1');

                      

                      return GestureDetector(
                          onTap: () {
                            showDialog(
                                context: context,
                                builder: (context) {
                                  return Dialog(
                                    child: Container(
                                      width: 220,
                                      height: 200,
                                      child: Image.network(
                                        '$doc',
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                  );
                                });
                          },
                          child: imageDialog());
                    },
                  ),
                  //_checkController();
                ),
              ),
relj7zay

relj7zay1#

如果你确定回复的格式一致,你可以试试这个。另外,为了确保你可以在每一步添加几个检查,以避免例外。

final String input = 'https://xuriti-prod-kyc.s3.ap-south-1.amazonaws.com/27AAACB2100P1ZX/1675230131972-download.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAXALYDECMNE2HOZS7%2F20230216%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20230216T063528Z&X-Amz-Expires=900&X-Amz-Signature=867311e96282f231406f46d5f2a5d11befe985b0b3a0ab161f69dfb3465b50a9&X-Amz-SignedHeaders=host';

final res = input.split('/').last.split('?').first.split('.').last;
a11xaf1n

a11xaf1n2#

只使用拆分和子字符串

String getExtension(url){
  url = url.split('?')[0];
  url = url.split('/').last;
  return url.contains('.') ? url.substring(url.lastIndexOf('.')+1) : "";
}

用途,

const url = 
   "https://xuriti-prod-kyc.s3.ap-south-1.amazonaws.com/27AAACB2100P1ZX/1675230131954-download-%281%29.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAXALYDECMNE2HOZS7%2F20230216%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Date=20230216T063528Z&X-Amz-Expires=900&X-Amz-Signature=59204e81b56cd325de1531a1e6c6a67c5030c76c0516b4719e40986e1ef1d917&X-Amz-SignedHeaders=host";

final extension = getExtension(url); // return 'jpeg'

if (extension == "jpeg"){
    // show image
}
else if(extension == "pdf") {
    // show download option
}
nnsrf1az

nnsrf1az3#

你可以在这里使用正则表达式来代替拆分:

String getExtension(url){
  final regexJpeg = RegExp(r'\.jpeg\?*');
  final regexPdf = RegExp(r'\.pdf\?*');
  if(regexJpeg.hasMatch(url)){
    return 'jpeg';
  }
  else if(regexPdf.hasMatch(url)){
    return 'pdf';
  } else{
    return 'unknown';
  }
}

相关问题