I need your help for an error I encounter in flutter dart - flutter

I have an application and I created a legin and logout page... and when I click on my application's logout button, I get an error like this " Null check operator used on a null value"*and when I point to the error, it tells me [1] :
https://i.stack.imgur.com/n0uJ8.pngentrez
import 'dart:async';
import 'dart:convert';
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:logger/logger.dart';
import '../db/db_auth_shared_preference.dart';
import '../network/app_urls.dart';
import '../models/auth.dart';
enum Status {
notLoggedIn,
loggedIn,
authenticating,
loggedOut,
notReet,
reseted,
resetting
}
//Help display the logs
var logger = Logger();
class AuthProvider with ChangeNotifier {
Auth? _auth;
Auth get auth => _auth!;
void setAuth(Auth auth) {
_auth = auth;
notifyListeners();
}
bool isAuth() {
if (_auth == null || auth.token == '') {
return false;
}
return true;
}
// Time before the token expires
Timer? _authTimer;
DateTime? _expiryDate;
String? username;
String? password;
// Set the status of the user to Not Logged In at the start of the app
Status _status = Status.notLoggedIn;
Status get status => _status;
// Change the status of the user
set status(Status value) {
_status = value;
notifyListeners();
}
// Log In the user
Future<Map<String, dynamic>> login(String email, String password) async {
Map<String, Object> results;
final Map<String, dynamic> loginData = {
'email': email,
'password': password
};
status = Status.authenticating;
logger.d("--- Authentication ---");
try {
Response response = await post(
Uri.parse(
"${AppUrl.login}? username=${loginData['email']}&password=${loginData['password']}"
),
);
logger.d('Login response : ${response.statusCode}');
// The Request Succeded
if (response.statusCode == 200) {
final Map<String, dynamic> responseData =
json.decode(utf8.decode(response.bodyBytes));
var requestStatus = responseData["status"];
if (requestStatus != 0) {
status = Status.notLoggedIn;
results = {'status': false, 'message': "La Connexion a échoué"};
} else {
// Get the status code of the request
Map<String, dynamic> authData = responseData["utilisateurJson"];
logger.d(authData);
_expiryDate = DateTime.now().add(const Duration(seconds: 3500));
//store user shared pref
Auth authUser = Auth.fromMap(authData,
timeToExpire: _expiryDate,
username: loginData['email'],
password: loginData['password']);
_expiryDate = authUser.expiryDate;
logger.wtf(_expiryDate);
//clear session data
AuthPreferences().removeAuth();
//store User session
AuthPreferences().saveAuth(authUser);
setAuth(authUser);
status = Status.loggedIn;
username = loginData["email"];
password = loginData["password"];
results = {
'status': true,
'message': 'Successful login',
'auth': authUser,
};
autoLogOut();
}
} else {
status = Status.notLoggedIn;
results = {'status': false, 'message': 'La Connexion a échoué'};
}
return results;
} catch (e) {
logger.e(e);
status = Status.notLoggedIn;
results = {
'status': false,
'message': "La Connexion avec le serveur a échoué"
};
return results;
} }
void autoLogOut() {
if (_authTimer != null) {
_authTimer!.cancel();
}
final timeToExpiry = _expiryDate!.difference(DateTime.now()).inSeconds;
_authTimer = Timer(Duration(seconds: timeToExpiry),
() async => await login(username!, password!));
}
// Log Out the User
void logOut() {
logger.d("--- User Logging Out ---");
AuthPreferences().removeAuth();
status = Status.loggedOut;
_expiryDate = null;
_auth = null;
logger.d("--- User Logged Out ---");
}
Future<Auth?> tryAutoLogin() async {
final authSession = await AuthPreferences().getAuth();
if (authSession == null) {
return null;
}
logger.d("The expiry time is : ${authSession.expiryDate}");
if (authSession.expiryDate.isBefore(DateTime.now())) {
login(authSession.username, authSession.password);
return authSession;
}
_expiryDate = authSession.expiryDate;
setAuth(authSession);
logger.d("SETTING THE USER");
autoLogOut();
return authSession;
}
}

Error Explanation: Bang operator(!) means that in flutter, when you use this operator, you are completely sure that variable is not going to be null in any case.
There are two ways to resolve it -
Use if conditional to confirm that variable is not null
Use null-aware or if-null operator ?? like
Auth get auth => _auth ?? Auth();
Since you didn't provide any error logs; based on attached image and as your cursor on line no 29, _auth variable is null. So before using ! make sure your variable is not null.

Related

how to login by API using Provider State management in Flutter

what is the correct way to do login/authentication using Rest API in flutter using provider,
every time i get the below error in my console,
I/flutter (18602): 500
I/flutter (18602): {"message":"data and hash arguments required"}
this is my provider page,:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class LoginController extends ChangeNotifier{
bool _isLoading = false;
bool get isLoading => _isLoading;
void loginDemo({
required String email,
required dynamic password,
}) async {
_isLoading = true;
notifyListeners();
String urls = 'https://elated-pink-hedgehog.cyclic.app/login';
final response = await http.post(Uri.parse(urls),
body: ({
"email":email,
"password":password
}));
if (response.statusCode == 200) {
print(response.body);
print('login successfull');
notifyListeners();
}else{
print(response.statusCode);
print(response.body);
notifyListeners();
}
notifyListeners();
}
}
this is where i call the login Function in UI:
Consumer<LoginController>(
builder: (context, value, child) =>
ElevatedButton(
onPressed:(){
if (emailController.text.isEmpty || passwordController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:Text('All Field Required')));
}else{
value.loginDemo(
email: emailController.text,
password: passwordController.text
);
}
},
child:Text('login')),
),
please help me to solve this. iam new to flutter and provider.
I think the error comes from the way you post your body do this instead
void loginDemo({
required String email,
required dynamic password,
}) async {
_isLoading = true;
notifyListeners();
final Map<String, dynamic> loginData = {
'email': email,
'password': password
};
Response response = await post(
Uri.parse('https://elated-pink-hedgehog.cyclic.app/login'),
body: json.encode(loginData),
headers: {'Content-Type': 'application/json'},
);
if (response.statusCode == 200) {
final Map<String, dynamic> responseData = json.decode(response.body);
//if you have a model class then inject it here
UserModel authUser = UserModel.fromJson(responseData);
_isLoading = false;
notifyListeners();
}else{
print(response.statusCode);
print(response.body);
notifyListeners();
}
notifyListeners();
}

How to dynamically save token after logging to shared prefernces

How to dynamically auth users and save tokens in shared pref?
I understood how to save token in sharedprefernces, but can't understand how to take it dynamically by login/password and pass token from it to sharedpref dynamically in loginWithToken(); beacuse I use this function for auth in
final httpConnectionOptions = HttpConnectionOptions(
accessTokenFactory: () => SharedPreferenceService().loginWithToken(),
and it is required only String
My code now is like that:
Here is request where I am making request to get token:
Future<String?> getToken(String password, String login) async {
String _email = "admin";
String _password = "123";
Map<String, String> headers = {
'Content-Type': 'application/json',
'accept': ' */*'
};
final body = {
'username': _email,
'password': _password,
};
var response = await http.post(
Uri.parse("http://mylink/login"),
headers: headers,
body: jsonEncode(body),
);
if (response.statusCode == 200) {
var value = jsonEncode(response.body);
return value;
}
return null;
}
here is I created logging logic:
final TextEditingController _loginController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
ElevatedButton(
onPressed: () async {
var username = _loginController.text;
var password = _passwordController.text;
var jwt = await ProviderService()
.getToken(password, username);
if (jwt != null) {
SharedPreferenceService().setToken(jwt);
Navigator.pushNamed(
context, '/mainPageAdmin');
} else {
displayDialog(context);
}
},
here is my shared pref. I can't understand how to put new token value in that string, after paaword and login was sent.
String tokens = 'dhjwhdwdwkjdhdkje';
Future<bool> getSharedPreferencesInstance() async {
_prefs = await SharedPreferences.getInstance().catchError((e) {
print("shared preferences error : $e");
return false;
});
return true;
}
Future setToken(String token) async {
await _prefs?.setString('token', token);
}
Future clearToken() async {
await _prefs?.clear();
}
Future<String> get token async => _prefs?.getString('token') ?? '';
Future<String> loginWithToken() async {
bool value = await getSharedPreferencesInstance();
if (value == true) {
setToken("Bearer $tokens");
// print(tokens);
}
return tokens;
}
Api Responce:
{
"$id": "1",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZ",
"user": {
"$id": "2"
}
}
Auth class I parsed:
Auth authFromJson(String str) => Auth.fromJson(json.decode(str));
String authToJson(Auth data) => json.encode(data.toJson());
class Auth {
Auth({
this.token,
this.user,
});
final String? token;
final User? user;
factory Auth.fromJson(Map<String, dynamic> json) => Auth(
token: json["token"],
user: User.fromJson(json["user"]),
);
Map<String, dynamic> toJson() => {
"token": token,
"user": user!.toJson(),
};
}
In your getToken function do this:
if (response.statusCode == 200) {
var value = jsonEncode(response.body) as Map<String, dynamic>;
await setToken(value['token']);
return value;
}

Get token auth value to another dart using sharedprefence

how to retrieve token variable from sharedprefence in flutter?
i am very new to implement api for my flutter project because previously I was only told to work on the frontend, i have saved auth token in login and here is my code to store token in sharedprefence
signIn(String email, password) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
Map data = {
'email': email,
'password': password
};
var jsonResponse = null;
var response = await http.post(Uri.parse("/api/login"), body: data);
if(response.statusCode == 200) {
jsonResponse = json.decode(response.body);
if(jsonResponse != null) {
setState(() {
_isLoading = false;
});
sharedPreferences.setString("token", jsonResponse['data']['token']['original']['token']);
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => HomePage()), (Route<dynamic> route) => false);
}
}
else {
setState(() {
_isLoading = false;
});
scaffoldMessenger.showSnackBar(SnackBar(content:Text("Mohon cek Email dan Password kembali!", textAlign: TextAlign.center,), backgroundColor: Colors.red,));
}
}
and here is the darts place that I want to call the token for auth in the post method
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:kiriapp/models/angkot.dart';
class AngkotProvider with ChangeNotifier {
late AngkotModel _angkot;
AngkotModel get angkot => _angkot;
set angkot(AngkotModel newAngkot) {
_angkot = newAngkot;
notifyListeners();
}
static Future<AngkotModel?> tambah(
String user_id,
String route_id,
String plat_nomor,
String pajak_tahunan,
String pajak_stnk,
String kir_bulanan) async {
try {
var body = {
'user_id': user_id,
'route_id': route_id,
'plat_nomor': plat_nomor,
'pajak_tahunan': pajak_tahunan,
'pajak_stnk': pajak_stnk,
'kir_bulanan': kir_bulanan,
};
print(body);
var response = await http.post(
Uri.parse('api/create'),
headers: {
'Authorization': 'Bearer $howtocallthetoken?,
},
body: body,
);
print(response.statusCode);
print(response.body);
if (response.statusCode == 201) {
return AngkotModel.fromJson(jsonDecode(response.body));
} else if (response.statusCode == 400) {
return AngkotModel.fromJson(jsonDecode(response.body));
}{
return null;
}
} catch (e) {
print(e);
return null;
}
}
}
thanks
To store something in shared preference we use setString function, just like you did. Now to retrieve it, you should use getString and it will return the token you stored earlier.
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String accessToken = sharedPreferences.getString("token");
var response = await http.post(
Uri.parse('api/create'),
headers: {
'Authorization': 'Bearer $accessToken',
},
body: body,
);
Don't forget to make the function async, and handle null values as the getString function might return token as null if not stored correctly.

How I can get value from class extends ChangeNotifier

[My code] . When I use User user = Provider.of(context, listen: false).user; => user null
My code here
class UserController extends ChangeNotifier {
User _user;
User get user => _user;
Future<void> getUser() async {
String token = await AppValue.getToken();
Uri uri = Uri.parse('http://20.89.111.129/api/user/getProfileUser');
Map<String, String> headers = {'Authorization': 'Bearer $token'};
try {
final response = await http.get(
uri,
headers: headers,
);
final data = jsonDecode(response.body);
User newUser = new User(
address: (data['address'] == null) ? '' : data['address'],
birthday: (data['birthday'] == null) ? '' : data['birthday'],
email: (data['email'] == null) ? '' : data['email'],
fullName: (data['fullName'] == null) ? '' : data['fullName'],
);
_user = newUser; // _user is not null
notifyListeners();
} catch (error) {
log('ST WRONG!');
throw (error);
}
}
}
// When I use User user = Provider.of(context, listen: false).user; => user null
I think you need to tell the provider the exact class you want to act on,
//so instead of this;
User user = Provider.of(context, listen: false).user;
// do this
User user = Provider.of< UserController>(context, listen: false).user;

How do i use StateNotifier riverpod to track the changes of enum value

I'm trying to use Riverpod stateNotifier to track the changes of an enum during user authentication to determine the appropriate screen to be displayed. Eg SignUp, SignIn, Homepage or the Authenticating screen but I get this error back in my named constructor:
The superclass 'StateNotifier' doesn't have a zero argument constructor.
Try declaring a zero argument constructor in 'StateNotifier', or explicitly invoking a different constructor in 'StateNotifier'.
I know that there something i don't understand here but i can't figure it out.
Here is my code:
enum Status {
unInitialized,
unauthenticated,
authenticating,
authenticated,
processing
}
class AuthWithEmailPassword extends StateNotifier<Status> {
AuthWithEmailPassword() : super(Status.authenticated);
Status _status = Status.authenticated;
// AuthWithEmailPassword();
UserServices _userServices = UserServices();
FirebaseAuth _auth;
UserModel _userModel;
User _user;
Status get status => _status;
User get user => _user;
UserModel get userModel => _userModel;
//Name consturctor of this class
#override
AuthWithEmailPassword.initialize()
: _auth = FirebaseAuth.instance{
_status = Status.unInitialized;
_auth.authStateChanges().listen((User value) async {
_status = Status.unInitialized;
if (value == null) {
_status = Status.unauthenticated;
print('user is signed out');
} else {
_userModel = await _userServices.getUserByUid(id: value.uid);
_status = Status.authenticated;
_user = value;
print('user signed in');
}
});
}}
Instead of using a named constructor, you could create an initialize function and call it in your StateNotifierProvider.
For example:
class AuthWithEmailPassword extends StateNotifier<Status> {
AuthWithEmailPassword() : super(Status.authenticated);
Status _status = Status.authenticated;
Status get status => _status;
final FirebaseAuth _auth = FirebaseAuth.instance;
User _user;
User get user => _user;
UserModel _userModel;
UserModel get userModel => _userModel;
bool _init = false;
late StreamSubscription _sub;
void initialize() {
if (_init) return;
_status = Status.unInitialized;
_sub = _auth.authStateChanges().listen(_listener);
_init = true;
}
#override
void dispose() {
_sub.cancel();
super.dispose();
}
Future<void> _listener(User? value) async {
_status = Status.unInitialized;
if (value == null) {
_status = Status.unauthenticated;
print('user is signed out');
} else {
_userModel = await _userServices.getUserByUid(id: value.uid);
_status = Status.authenticated;
_user = value;
print('user signed in');
}
}
}
final authWithEmailPasswordProvider = StateNotifierProvider.autoDispose<AuthWithEmailPassword, Status>((_) {
return AuthWithEmailPassword()..initialize();
});
Thanks to Alex Hartford who helped me with a solution when i was desperate on this issue. I have finally been able to figure out another solution also and maybe someone might like it.
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_ecom/controle/userServices.dart';
import 'package:flutter_ecom/models/userModel.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
enum Status {
unInitialized,
unauthenticated,
authenticating,
authenticated,
}
final authStatus = StateNotifierProvider<AuthWithEmailPassword, Status>(
(ref) => AuthWithEmailPassword.initialize());
class AuthWithEmailPassword extends StateNotifier<Status> {
FirebaseFirestore firestore = FirebaseFirestore.instance;
UserServices _userServices = UserServices();
// Status state = Status.unInitialized;
FirebaseAuth _auth;
UserModel _userModel;
User _user;
// Status get status => _status;
User get user => _user;
UserModel get userModel => _userModel;
String _error;
String get error => _error;
//Name consturctor of this class
AuthWithEmailPassword.initialize()
: _auth = FirebaseAuth.instance,
super(Status.unInitialized) {
_auth.authStateChanges().listen((User value) async {
await Future.delayed(
const Duration(milliseconds: 4000),
);
if (value == null) {
state = Status.unauthenticated;
print('user is signed out');
} else {
_userModel = await _userServices.getUserByUid(id: value.uid);
state = Status.authenticated;
_user = value;
print('user signed in');
}
});
}