Home > Software design >  How to send a post request in clean architecture?
How to send a post request in clean architecture?

Time:09-29

Do not rush to downgrade the question, it is not as easy for me as the usual sending of a request... And then more details

How is a get request sent? The request occurs during the build through the BlockBuilder (at least for me it happens like this, there was no need to hang up a click request), and there, later, the model is filled with magic data.

For example -

return BlocProvider<BannerCubit>(
        create: (context) => sl<BannerCubit>()..loadBanner(),
      child: Scaffold(
        body: BannerCarousels(),
      ),
    );

class BannerCarousel -

Widget build(BuildContext context) {
    return BlocBuilder<BannerCubit, BannerState>(
        builder: (context, state) {
          List<BannerEntity> banner = [];
          if(state is BannerLoading) {
            return _loadingIndicator();
          } else if (state is BannerLoaded) {
            banner = state.bannerList;
          }
          print('The following data for the banner came to me from the server: $banner');
          return CarouselSlider.builder(

As you can see, I'm not making a request here. In general, this request has nobody. data source file:

Future<List<ProductsModel>> getAllProducts() async {
    final response = await client.get(
        Uri.parse(ConfigUrl.home),
        headers: {'Content-Type': 'applications/json'}
    );
    if(response.statusCode == 200) {
      final products = json.decode(response.body);
      print('For banner: $products');
      return (products['products'] as List).map((e) => ProductsModel.fromJson(e)).toList();
    } else {
      throw ServerException();
    }

And now I have a question. How can I pass data to the request body from my TextFormField widget, or rather from its controller? Or should I form a request directly in the button widget that makes the request?

PS: I recently started to study clean architecture and faced such a problem as not understanding how to send a post request, fill in its body and parse the response.

I would be very happy if I saw some code that would be understandable to a beginner.

CodePudding user response:

I'm not a flutter dev, but I understand you are issue. you are trying to pass message between two different items and I assumed it is message passing problem.

Google keyword : Message passing in flutter

https://ognjen.io/flutter-message-passing-example/

CodePudding user response:

I am using GetX arch, which is another good architecture, where we use generally HTTP package,

A good example of post request you can find below.

Future<http.Response> createCustomer(String name, String jobTitle) {
    return http.post(
        Uri.parse('https://reqres.in/api/users'),
        headers: <String, String>{
            'Content-Type': 'application/json; charset=UTF-8',
        },
        body: jsonEncode(<String, String>{
            'name': name,
            'job': jobTitle
        }),
    );
}

You Can find HTTP package here Pub.dev

  • Related