TimeToCode
TimeToCode

Reputation: 1838

Unhandled Exception: NoSuchMethodError: The getter 'data' was called on null

i am creating a login form in flutter and using mongodb as database.

here is my authentication file code

import 'dart:convert';

import 'package:attendance_system_app/Screens/Dashboard/Employee_Dashboard.dart';
import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;

class AuthService{
  Dio dio=new Dio();
var localhost_url="http://10.0.2.2:8000/login";
var heroku_Url="https://attendance-demo.herokuapp.com/login";
 
  login(name,password) async{
    Map<String,dynamic> data={
      'username':name,
      'password':password
    };
 await dio
    .post(localhost_url,data: json.encode(data))
      .then((onResponse){
        print(onResponse.data);
        // EmployeeDashboard();
      }).catchError((onerror){
        print(onerror.toString());
    });

  }
}

and here i call the above code on login button

 RoundedButton(text:"                          Login",  press: () {
 AuthService().login(name,password).then((value){
                      if(value.data['success']){
                        token=value.data['token'];
                        Fluttertoast.showToast(msg: 'Authenticated',
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        backgroundColor: Colors.red,
                        textColor: Colors.white,
                        fontSize: 16.0);
                    }
//here i call the dashboard when credentials are correct
                  Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => EmployeeDashboard()),
                  );

my server is connected successfully and api is working perfectly on postman. when i enter the username and password i got the details related to that username as output on terminal plus i am getting this error too.

ERROR:

Unhandled Exception: NoSuchMethodError: The getter 'data' was called on null.
E/flutter (13740): Receiver: null
E/flutter (13740): Tried calling: data

here is the snap of output

enter image description here

please help if anyone know the solution and where i am doing wrong! thank you.

Upvotes: 1

Views: 1160

Answers (3)

TimeToCode
TimeToCode

Reputation: 1838

this is what I had done!

Future save() async {
    Dio dio=new Dio();
    var data={
      'username': user.username,
      'password': user.password

    };
    await dio
    .post(localhost_url,data: json.encode(data))
      .then((onResponse){
        print(onResponse.data);
        
      }).catchError((onerror){
        print(onerror.toString());
    });

    Navigator.push(
        context, new MaterialPageRoute(builder: (context) => EmployeeNavigation()));
  }

  User user = User('', '');

User.dart file

class User {
  String username;
  String password;
  User(this.username, this.password);
}

login button code

RoundedButton(text:"Login",  press: () {
                  save();
}

please suggest is this a good to done this!

Upvotes: 0

Y.B. Vamsidhar Reddy
Y.B. Vamsidhar Reddy

Reputation: 162

Let me make it simple in login function You didnt returned any value

When you return some data in login function then

in your code value has some data

without anydata you are calling value.data['sucess']

so return some value in your login funtion

Upvotes: 3

Mol0ko
Mol0ko

Reputation: 3298

Just return response from then handler:

 await dio
    .post(localhost_url,data: json.encode(data))
      .then((onResponse){
        print(onResponse.data);
        return onResponse;      // <--- HERE
      }).catchError((onerror){
        print(onerror.toString());
    });

I would also recommend you to use explicit types in your code instead of dynamic where possible. Code will be more readable and it would be easier to work with objects. Example:

Response<Map<String, dynamic>> login(String name, String password) async{
...

Upvotes: 2

Related Questions