flutter Login Api Screen is navigation even email is wrong or right ....! thank you

0

i'am new to flutter and i'm stuck here since 5 days ! i have api and this api is accessing properly and the thing is that image is navigating ! plz look below code and give me any solution on it... i very helpfull if some gives me this soln ! .....
i folliowed all tutorial and souce code i dt have any capacity to look on this plz help me m stuck in thhis....! my Question is screen is navigating even email password true wrong i m new to flutter... i tried many tyme m goonna canfuse now plzzzzzzz guyssss

  import 'dart:convert';
  import 'package:flutter/material.dart';
  import 'package:google_fonts/google_fonts.dart';
  import 'package:http/http.dart' as http;
  import 'package:shared_preferences/shared_preferences.dart';
  import 'package:sms_snd/data.dart';
  import 'new.dart';



  void main() {
 runApp(MyApp());
 }
 class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 return MaterialApp(
  debugShowCheckedModeBanner: false,
  home: Loginpage(),
 );
}
}

class Loginpage extends StatefulWidget {
@override
_LoginpageState createState() => _LoginpageState();
}

class _LoginpageState extends State<Loginpage> {

  Signup(String email,String Password) async{
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
       "APIKEY":"QVBAMTIjMllIRC1TREFTNUQtNUFTRksyMjE4Ng==",
       "SECRETKEY":"MjQ1QDEyIzJZSEQtODVEQTJTM0RFQTg1Mz1JRTVCNEE1ODY=",
       "Email": email,
       "Password":Password
   };
    var jsonResponse = null;
    var response = await http.post("http://services.edbrix.net/auth/login",
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      },
      body: jsonEncode(data));
    if (response.statusCode == 200){
      jsonResponse = json.decode(response.body);
      print("Response status : ${response.statusCode}");
      print("Response status : ${response.body}");
      if(jsonResponse != null){
        sharedPreferences.setString("AccessToken", jsonResponse["AccessToken"]);
        return Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder:
            (BuildContext context) => newapp()),
                (Route<dynamic> route) => false);
        }

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

      }
    }
  }
  String _email;
  bool _isLoading = false;
  String _pass;
 final TextEditingController emailController = new TextEditingController();
 final TextEditingController passwordController = new TextEditingController();

 @override
 Widget build(BuildContext context) {
 return Scaffold(
  backgroundColor: Colors.white10,
  body:_isLoading ? Center(child: CircularProgressIndicator()) : Form(
    child: Stack(
      children: <Widget>[
        Positioned(
          left: 40,
          right: 40,
          top: 250,
          child: Center(
            child: Text(
              'Welcome!',
              style: GoogleFonts.nunito(
                fontSize: 31,
                color: const Color(0xfff58634),
                fontWeight: FontWeight.w700,
                height: 1.1290322580645162,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ),
        Positioned(
          left: 30,
          right: 30,
          top: 320,
          child: SizedBox(
            height: 50,
            width: 170,
            child: TextFormField(
              controller: emailController,
              cursorColor: Colors.black,
              style: TextStyle(color: Colors.white70),
              decoration: InputDecoration(
                icon: Icon(Icons.email, color: Colors.white70),
                hintText: "Email",
                border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.white70)),
                hintStyle: TextStyle(color: Colors.white70),
              ),
            ),
          ),
        ),
        Positioned(
          left: 30,
          right: 30,
          top: 380,
          child: SizedBox(
            height: 50,
            width: 170,
            child: TextFormField(
              controller: passwordController,
              cursorColor: Colors.black,
              style: TextStyle(color: Colors.white70),
              decoration: InputDecoration(
                icon: Icon(Icons.lock, color: Colors.white70),
                hintText: "Password",
                border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.white70)),
                hintStyle: TextStyle(color: Colors.white70),
              ),
            ),
          ),
        ),
        Positioned(
          top: 460,
          right: 30,
          child:SizedBox(
            height: 45,
            child: RaisedButton(
              onPressed:

                  () {
                Signup(emailController.text.toString(), passwordController.text.toString());
              },
              color: const Color(0xfff58634),
              child: Text(
                "SEND OTP",style:
              GoogleFonts.nunito( color: const Color(0xffffffff),
                  fontSize: 12
              ),
              ),
            ),
          ),
        )
      ],
    ),
    ),
    );
    }
    }
api
flutter
authentication
asked on Stack Overflow Nov 11, 2020 by Ah Zeem khatib

1 Answer

0

You are checking if jsonResponse != null and even if email is not correct, API call will still return some json data,

{
  "Error": {
        "ErrorCode": "E1027",
        "ErrorMessage": "Invalid Email."
    }
}

you need to check if jsonResponse != null && ! jsonResponse.containsKey("Error")

answered on Stack Overflow Nov 11, 2020 by Raghav Bhardwaj

User contributions licensed under CC BY-SA 3.0