Home > database >  How can I run register and login in Flutter with api?
How can I run register and login in Flutter with api?

Time:11-28

I have written Sign up and Login with the Laravel (API), but I want this operation to be performed in Flutter. I do not know which way to use it.

For example, in react we use axios for get or post data. In Flutter what is the equivalent?

CodePudding user response:

Dart has an http package that you can use to make api requests. The flutter [documentation] walks through how you can add that package and use it to make requests from your app.

import 'package:http/http.dart' as http;
Future<http.Response> fetchAlbum() {
  return http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
}

You'll also need add the http package as a dependency and add internet permissions in AndroidManifest.xml

CodePudding user response:

There are some libraries in pub.dev, such as dio and http. Those are the common libraries that I use, and they also provide methods to set parameters and other things.

Example use:

Dio client = DioClient().getClient(base_url, 15000, 15000);
client.options.headers = {
  "Accept": "application/json",
  "Content-Type": "application/json",
};
try {
  var response =
      await client.get(link_after_base_url_before_parameters, queryParameters: params);
  if (response.statusCode == 200) {
    ResponseDto responseDto = ResponseDto.fromJson(response.data);
  }
} catch(e) {}

Check this out:

https://pub.dev/packages/dio

https://pub.dev/packages/http

  • Related