Http.post if succes move to new screen

  1. Summary of my problem

I can successfully post data using dart to my database but i am having some issues navigating to a new page after successful submission.

i need to decode the response and when the response equals “succes” i would like to navigate to the login page.

the response.body contains flutter: [“success”] How can i send it to another screen in this particular situation?

  1. What have I tried so far -
Future navigateToLogin(context) async {
 Navigator.push(context, MaterialPageRoute(builder: (context) => login()));
}

if(reponse.body == "succes"){
navigateToLogin(context);
}
  1. Here’s the minimum code you would need to reproduce the problem -
import 'package:flutter/material.dart';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'login.dart';
import 'dart:async';
import 'dart:convert';

Future navigateToRegister(context) async {
  Navigator.push(context, MaterialPageRoute(builder: (context) => login()));
}



class register  extends StatelessWidget  {

  TextEditingController emailController = TextEditingController();
  TextEditingController pwController = TextEditingController();
  TextEditingController repeatpwController = TextEditingController();
  TextEditingController firstnameController = TextEditingController();
  TextEditingController lastnameController = TextEditingController();



  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Register'),
      ),

        body: Padding(
            padding: EdgeInsets.all(10),
            child: ListView(
              children: <Widget>[
                Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.all(10),
                    child: Text(
                      'Create Account',
                      style: TextStyle(
                          color: Colors.blue,
                          fontWeight: FontWeight.w500,
                          fontSize: 30),
                    )),
                Container(
                   padding: EdgeInsets.fromLTRB(10, 10, 10, 0),

                  child: TextField(
                    obscureText: false,
                    controller: emailController,

                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Email',

                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                  child: TextField(
                    obscureText: false,
                    controller: firstnameController,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Firstname',
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                  child: TextField(
                    obscureText: false,
                    controller: lastnameController,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Lastname',
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                  child: TextField(
                    obscureText: true,
                    controller: pwController,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Password',
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                  child: TextField(
                    obscureText: true,
                    controller: repeatpwController,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Repeat password',
                    ),
                  ),
                ),

                Container(
                    height: 80,
                    padding: EdgeInsets.fromLTRB(10, 20, 10, 0),
                    child: RaisedButton(
                      textColor: Colors.white,
                      color: Colors.blue,
                      child: Text('Register'),
                      onPressed: () {




                        if(pwController.text == repeatpwController.text) {
                          insertData(
                              firstnameController.text, lastnameController.text,
                              pwController.text, emailController.text);
                        }else{
                          print('error');
                        }
                      },
                    )),
              ],
            )));
  }

  Future insertData(var firstname, var lastname, var pwcontroller, var email) async {
    var url = "https://thisismyurl.php";


    final response = await http.post(url, body: {

      "firstname": firstname,
      "lastname": lastname,
      "password": pwcontroller,
      "email": email
    });
    print (response.body);

  }

}

Hey! Try changing the below to:

if(reponse.body['flutter'] == "succes"){
navigateToLogin(context);

Also, if there is scope of modification I would highly recommend to change the response to {"success":true}

And then check

if(reponse['success']){ navigateToLogin(context); }

Boolean checking is more reliable and less expensive. Before this check though I would also recommend you to check the statusCode. Because if there is no response from server then this check will throw an error and your code will get stuck. Check if statuscode is between 200 and 300. This is the general success code range.

Hope this helps.

Hi, thanks for your suggestion. i changed the json response to flutter: {success: true}

I do get the following error though
The operator ‘[]’ isn’t defined for the type ‘Response’.

also i get an error on (context). undefined name context.

when placing json.decode(response.body) in a seperate var i get the error Unhandled Exception: type String is not a subtype of type ‘bool’

 if (response.statusCode < 300 && response.statusCode >= 200) {
   //check for success status code
    Map<String, dynamic> responseBody = json.decode(response.body); //decode response 
  if(responseBody["success"]) {
     //SUCCESS
   }
  }

Hope the above code snippet helps you!

1 Like