Flutter : Post request with credentials

后端 未结 1 933
天涯浪人
天涯浪人 2021-01-03 12:39

How can I make post requests from flutter.I need to authenticate a user with his email address and password. Please help

tried with the following code



        
1条回答
  •  别那么骄傲
    2021-01-03 13:32

    I use

    import 'dart:async' show Future;
    import 'dart:io'
        show HttpClient, HttpClientBasicCredentials, HttpClientCredentials;
    
    import 'package:http/http.dart' show BaseClient, IOClient;
    
    typedef Future HttpAuthenticationCallback(
        Uri uri, String scheme, String realm);
    
    HttpAuthenticationCallback _basicAuthenticationCallback(
            HttpClient client, HttpClientCredentials credentials) =>
        (Uri uri, String scheme, String realm) {
          client.addCredentials(uri, realm, credentials);
          return new Future.value(true);
        };
    
    BaseClient createBasicAuthenticationIoHttpClient(
        String userName, String password) {
      final credentials = new HttpClientBasicCredentials(userName, password);
    
      final client = new HttpClient();
      client.authenticate = _basicAuthenticationCallback(client, credentials);
      return new IOClient(client);
    }
    
    final http =
        createBasicAuthenticationIoHttpClient(_config.userName, _config.password);
    
    http.get(...)
    

    0 讨论(0)
提交回复
热议问题