skip to Main Content

this is the signup1.dart function which is taking controllers from signup1 to signup2

void navigateToSignup2() {
    if (emailController.text.isNotEmpty &&
        fullNameController.text.isNotEmpty &&
        phoneNumberController.text.isNotEmpty &&
        passwordController.text.isNotEmpty) {
      // Passing data from Signup1 to Signup2
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => SignUp2(
            fullName: fullNameController.text,
            email: emailController.text,
            phone: phoneNumberController.text,
            password: passwordController.text,
          ),
        ),
      );
    } else {
      setState(() {
        _isNotValidate = true;
      });
    }
  }

this is signup2.dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../config.dart';
import '../uihelperbutton/textfield_Name.dart';
import 'signup_1.dart';

class SignUp2 extends StatefulWidget {
  final String fullName;
  final String email;
  final String phone;
  final String password;

  const SignUp2({
    super.key,
    required this.fullName,
    required this.email,
    required this.phone,
    required this.password,
  });

  @override
  State<SignUp2> createState() => _SignUp2State();
}

class _SignUp2State extends State<SignUp2> {
  final TextEditingController businessNameController = TextEditingController();
  final TextEditingController informalNameController = TextEditingController();
  final TextEditingController streetAddressController = TextEditingController();
  final TextEditingController cityController = TextEditingController();
  final TextEditingController zipCodeController = TextEditingController();
  final TextEditingController stateController = TextEditingController();

  bool _isNotValidate = false;

  void registerUser() async {
    if (businessNameController.text.isNotEmpty &&
        informalNameController.text.isNotEmpty &&
        streetAddressController.text.isNotEmpty &&
        cityController.text.isNotEmpty &&
        zipCodeController.text.isNotEmpty &&
        stateController.text.isNotEmpty) {

      var regBody = {
        "full_name": widget.fullName,
        "email": widget.email,
        "phone": widget.phone,
        "password": widget.password,
        "business_name": businessNameController.text,
        "informal_name": informalNameController.text,
        "address": streetAddressController.text,
        "city": cityController.text,
        "state": stateController.text,
        "zip_code": zipCodeController.text,
      };

      print("Request body: $regBody"); // Log the request body
      print(jsonEncode(regBody));


      try {
        var response = await http.post(
          Uri.parse(registration),
          headers: {"Content-Type": "application/json"},
          body: jsonEncode(regBody),
        );

        print("Response status: ${response.statusCode}");
        print("Response body: ${response.body}");

        if (response.statusCode == 200) {
          print("User registered successfully");
          // Navigate to success page or show success message
        } else {
          print("Error: ${response.statusCode}, ${response.body}");
          // Show an error dialog or message to the user
        }
      } catch (e) {
        print("An error occurred: $e");
      }
    } else {
      setState(() {
        _isNotValidate = true;
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.only(left: 30.0, top: 40),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                'FarmerEats',
                style: TextStyle(fontSize: 16),
              ),
              SizedBox(height: 25,),
              Text('Signup 2 of 4',style: TextStyle(color: Colors.grey),),
              SizedBox(
                height: 8,
              ),
              Text(
                'Farm Info!',
                style: TextStyle(fontSize: 35, fontWeight: FontWeight.bold),
              ),
              SizedBox(
                height: 30,
              ),
              TextfieldName(Image: Image.asset('assets/images/business_name_logo.png',width: 20), hintText: 'Business Name',controller: businessNameController,),
              SizedBox(height: 20),
              TextfieldName(Image: Image.asset('assets/images/simely_logo.png',width: 20,), hintText: 'Informal Name',controller: informalNameController,),
              SizedBox(height: 20),
              TextfieldName(Image: Image.asset('assets/images/address_logo.png',width: 20,), hintText: 'Street Address',controller: streetAddressController,),
              SizedBox(height: 20),
              TextfieldName(Image: Image.asset('assets/images/location_logo.png',width: 20,), hintText: 'City',controller: cityController,),
              SizedBox(height: 20,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    height: 50,
                    width: 125,
                    decoration: BoxDecoration(
                      color: Colors.grey.shade200,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(left: 8.0, right: 14),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Expanded(
                            child: TextField(
                              controller: stateController,
                              style: TextStyle(color: Colors.black),
                              textInputAction: TextInputAction.next,
                              autofocus: false,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: 'State',
                                hintStyle: TextStyle(color: Colors.grey),
                              ),
                            ),
                          ),
                          Image.asset(
                            'assets/images/Polygon [email protected]',
                            height: 10,
                          ),
                        ],
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(right: 28.0),
                    child: Container(
                      height: 50,
                      width: 190,
                      child: TextField(
                        controller: zipCodeController,
                        style: TextStyle(color: Colors.black),
                        textInputAction: TextInputAction.next,
                        autofocus: false,
                        decoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                            borderSide: BorderSide(color: Colors.grey, width: 0.0),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(20),
                          ),
                          hintText: 'Enter Zipcode',
                          hintStyle: TextStyle(color: Colors.black12),
                          fillColor: Colors.grey.shade200,
                          filled: true,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 160),

              Padding(
                padding: const EdgeInsets.only(right: 30.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: Image.asset(
                        'assets/images/arrow_back.png',
                        height: 18,
                      ),
                    ),
                    Container(
                      width: 225,
                      height: 45,
                      decoration: BoxDecoration(
                        color: Color(0xffD5715B),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Center(
                        child: GestureDetector(
                          onTap: registerUser,
                          child: Text(
                            'Signup',
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.w500,
                              fontSize: 18,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(height: 30),
            ],
          ),
        ),
      ),
    );
  }
}

I have tried –

headers: {"Content-Type": "application/json"},
before it was
headers: {"Content-Type": "Application/json"},
my mobile device and laptop are connected to same wifi network
added port and ip manually to android studio
i have also checked with postman and the backend is working correctly and data is being stored in the mongodb.

2

Answers


  1. Chosen as BEST ANSWER

    it got solved i was missing the Future Function

    Future<void> registerUser() async {
    var regBody = {
      "full_name": widget.fullName,
      "email": widget.email,
      "phone": widget.phone,
      "password": widget.password,
      "business_name": businessNameController.text,
      "informal_name": informalNameController.text,
      "address": streetAddressController.text,
      "city": cityController.text,
      "state": stateController.text,
      "zip_code": zipCodeController.text
    };
    
    var response = await http.post(
      Uri.parse('http://192.168.1.6:3000/registration'),
      headers: {"Content-Type": "application/json"},
      body: jsonEncode(regBody),
    );
    
    if (response.statusCode == 200) {
      // Success - handle accordingly
      print("Registration successful");
    } else {
      // Failure - handle accordingly
      print("Registration failed");
    }
    

    }


  2. I don’t get your error well ,
    but if your case is:
    you are running the backend on the same laptop and you are trying to access it from flutter emulator all you have to do http://10.0.2.2:5237/{your end point}
    the port number just all what you have to change according to your backend port this because android security refuse to access the local urls also check if the function is called try to debug it

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search