Search code examples
flutterdarturi

How to get only directory from URI


A URL with parameters is passed to the URI variable. The protocol and domain may be https://example.com or localhost:xxxxxx. In either case, the subsequent directory and file names are the same.

In this case, is there a way to get it only from the directory?

example:

 final uri = Uri.parse("localhost:XXXX/category1/category2/?id=AAAA")

 // or it might be 

 final uri = Uri.parse("https://example.com/category1/category2/?id=AAAA")


 final result = doMagic(uri); // this is what I ask
 print(result); // category1/category2/?id=AAAA


Solution

  • For this:

    final uri = Uri.parse("https://example.com/category1/category2/?id=AAAA");
    

    you can get:

    final path = uri.path; // '/category1/category2/'
    final query = uri.query; // 'id=AAAA'