skip to Main Content

Below is the response where I am getting image path.the file can be pdf or jpeg or any other format. for image i am showing it in dialogue and for pdf I have to download it to the user’s device.For that first I have to check where the file is pdf or not for that i wants to get the extension of file format like pdf or jpeg from whole path (shown in 0th and 1st). How to do it using spilt string I have tried several ways but unable to get the result.Please guide me regarding it.

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"

Below is my code where i tried to split and get the file format:

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();
                ),
              ),

3

Answers


  1. If you are sure response will have consistent format you can try this. Also to be sure you may add a couple of checks on each step to avoid exceptions.

    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;
    
    Login or Signup to reply.
  2. just use split and substring

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

    Usage,

     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
    }
    
    Login or Signup to reply.
  3. Instead of splitting you can use regex here:

    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';
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search