How to capture the response coming in url in Flutter

Viewed 595

I am giving post request to the api and I am getting the response code in the redirectUrl as below https://localhost/?code=hfdh. I want to extract the code from the url. Is this possible to do. If yes then how it is possible in Flutter? If not then how to make this possible because this code I want to use to make another request to get access token.

My Flutter code is as follows:

class WebViewContainer extends StatefulWidget {
  final url;

  WebViewContainer(this.url);

  @override
  _WebViewContainerState createState() => _WebViewContainerState(this.url);
}

class _WebViewContainerState extends State<WebViewContainer> {
  var _url;
  final _key = UniqueKey();

  _WebViewContainerState(this._url);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: header(context, strTitle: 'verify yourself'),
      body: Column(
        children: [
          Expanded(
              child: WebView(
                  key: _key,
                  javascriptMode: JavascriptMode.unrestricted,
                  zoomEnabled: false,
                  initialUrl: _url,
                  navigationDelegate: (request) {
                    if (request.url.contains('localhost')) {
                      print('Trying to open app');
                      Navigator.pop(context); // Close current window
                      return NavigationDecision.navigate;
                    } else if (request.url.contains('youtube.com')) {
                      print('Trying to open Youtube');
                      return NavigationDecision.prevent; // Prevent opening url
                    } else {
                      return NavigationDecision.navigate; // Default decision
                    }
                  },
                onPageFinished: (String url) {
                    print('page started loading: $url'); //here I am getting https://localhost/?code=hfdh
                },
              ))
        ],
      ),
    );
  }
}

I have tried below solution to access the path segments like this.

var uri = Uri.dataFromString('http://localhost/?code=hfdh');
  var code = uri.pathSegments.last;
  print(code); // Here I am getting 'localhost'

But I want to extract code from Url.

Note:- This code is the responseCode when user authenticate themselves and onClick of the button it redirects to localhost url and at the end in response server provides the unique string which is ?code=hfdh

So I want this code saved in the app in variable or in any other way so that I can use this code in app.

0 Answers
Related