Configuration
Learn about the configuration options and advanced features of GetConnect.
Overview
GetConnect
provides several advanced features like interceptors, custom decoders, connection time-outs, and more. In this lesson, we’ll explore these and learn how they can help us create robust and maintainable apps.
Extending GetConnect
As we saw in the previous lessons, one way to use GetConnect
is to create an instance. This class supports the complete life cycle, like GetxController
, so we can conveniently place our server call methods.
class HTTPService extends GetConnect {@overridevoid onInit() {super.onInit();// configure everything here}}
We can then initialize this using Get.find
or bind it to a route, just as we do with the controllers. This makes our code more organized.
Base URL
We can set a base URL so that we don’t have to specify the same URL in every request.
baseUrl = 'https://jsonplaceholder.typicode.com';
Once set, this URL is automatically assigned to all requests and even web sockets. Now whatever we specify in the url
field is appended to the baseUrl
.
await get('/posts/1');
Content type
In most of our requests, the content type mostly remains the same. So instead of repeating it in all requests, we can set defaultContentType
.
defaultContentType = 'application/json; charset=utf-8';
Now this is used by default, but we can still override it in the request method itself.
Decoder
We can override the default decoder so that the data we get in from response body gets deserialized as per our model structure.
defaultDecoder = PostModel.fromJson;
Once again, decoder can be overridden in the method itself.
User agent
User agent is a string sent to the server with each request that provides it information like app’s name, version, OS, device, etc. We’re free to customize it as we like and we can even decide to not send any user agent.
userAgent = 'MyCustomApp/1.0 (Android 11; SM-G991B Build/RP1A.200720.012)';sendUserAgent = true;
Setting sendUserAgent
to false
will prevent it from being sent.
Certificates
We can give the request permission to allow auto-signed certificates by setting allowAutoSignedCert
.
allowAutoSignedCert = true;
We also get to load the certificates that we trust.
trustedCertificates = <TrustedCertificate>[certificate1, certificate2];
TrustCertificate
is a minimal class that takes the certificate file bytes.
Credentials
Credentials such as cookies, certificates, etc. are essential in case of cross-origin requests. It is necessary only in case of Flutter web apps because cross-origin request is a function of browser.
We can set ...