Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

json - Flutter Registration with API

I successfully can login with my API connection also can display the data in my app.

I follow the same method for the registration to send data in API So that I can login.

But the registration is not happening, although it didn't show any error. My SnackBar showing You are already registered . But When I try to login with the data which I provided in registration form, its showing user not found( Obvious reason registration process didn't perform). Is there any problem with my API MODEL?

Here is my Parameters

 {

    "Email":"qwer@gmail.com",
    "Mobile":"1237891",
    "Password":"9991",
    "RetypePassword":"9991"

}

And here is the Json response

 {
    "Status": "1",
    "Message": "You are registered successfully",
    "UserData": {
        "Name": "qwer@gmail.com",
        "EncUserId": "IS0QOCrLby8Ft1kbkzn/mg=="
    }
}

After that I created a registration form, when user click the Register button it's run my API function registrationOfuser()

Future <void> registrationOfuser(String  email, contact, pass,conpass) async{
    var jsonResponse ;
    Map data = {
      
      'Email': email,
      'Mobile': contact,
      'Password': pass,
      'RetypePassword': conpass,
    };
    print(data);

     String body = json.encode(data);
    var url = 'http://myurl/home/djkjkfjkjfwkjfkwjkjfkjwjfkwjfkwf';
    var response = await http.post(
      url,
      body: body,
      headers: {
        "Content-Type": "application/json",
        "accept": "application/json",
        "Access-Control-Allow-Origin": "*"
      },
    );

    print(response.body);
    print(response.statusCode);

    if (response.statusCode == 200) {
       jsonResponse = json.decode(response.body.toString());
        ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content:Text(" ${jsonResponse['Message']}"))) ;      
         
      //Or put here your next screen using Navigator.push() method
      print('success');
    } else {
      print('error');
    }



}

And here is my MODEL

    class RegistrationApiResponse {
    RegistrationApiResponse({
        required this.status,
        required this.message,
        required this.userData,
    });

    String status;
    String message;
    //UserData userData;
    UserData? userData;

    factory RegistrationApiResponse.fromJson(Map<String, dynamic> json) => RegistrationApiResponse(
        status: json["Status"],
        message: json["Message"],
         userData: json["UserData"] == null? null:UserData.fromJson(json["UserData"] as Map<String, dynamic>),
        
       


}

class UserData {
    UserData({
        required this.name,
        required this.encUserId,
    });

    String name;
    String encUserId;

    factory UserData.fromJson(Map<String, dynamic> json) => UserData(
        name: json["Name"],
        encUserId: json["EncUserId"],
    );

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to below code help:

Create TextEditingController

  final TextEditingController email = new TextEditingController();
  final TextEditingController contact = new TextEditingController();
  final TextEditingController password = new TextEditingController();
  final TextEditingController conpassword = new TextEditingController();

Create one function for registration

register(String  email, contact, pass,conpass) async {
    Map data = {
      
      'Email': email,
      'Mobile': contact,
      'Password': pass,
      'RetypePassword': conpass,
    };
    print(data);

    String body = json.encode(data);
    var url = 'Your url here';
    var response = await http.post(
      url,
      body: body,
      headers: {
        "Content-Type": "application/json",
        "accept": "application/json",
        "Access-Control-Allow-Origin": "*"
      },
    );
    print(response.body);
    print(response.statusCode);
    if (response.statusCode == 200) {
      //Or put here your next screen using Navigator.push() method
      print('success');
    } else {
      print('error');
    }
  }

Create Button

ElevatedButton(
child:Text('Register'),
onPressed:(){
   register(email.text, contact.text, password.text, conpassword.text);
   },
),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...