check null safety
This commit is contained in:
@@ -9,7 +9,7 @@ import 'package:fcs/domain/entities/user.dart';
|
||||
import 'package:fcs/domain/exceiptions/signin_exception.dart';
|
||||
import 'package:fcs/helpers/api_helper.dart';
|
||||
import 'package:fcs/helpers/firebase_helper.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart' as fb;
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class AuthFb {
|
||||
@@ -18,17 +18,17 @@ class AuthFb {
|
||||
static final AuthFb instance = AuthFb._();
|
||||
AuthFb._();
|
||||
|
||||
StreamController<User> controller;
|
||||
static final FirebaseAuth _fb = FirebaseAuth.instance;
|
||||
static String _verificationId;
|
||||
late StreamController<User?> controller;
|
||||
static final fb.FirebaseAuth _fb = fb.FirebaseAuth.instance;
|
||||
static String _verificationId = '';
|
||||
|
||||
Future<fcs.AuthResult> sendSmsCodeToPhoneNumber(String phoneNumber) {
|
||||
Completer<fcs.AuthResult> completer = Completer();
|
||||
bool codeSentCompleted = false;
|
||||
|
||||
final PhoneVerificationCompleted verificationCompleted =
|
||||
(AuthCredential credential) async {
|
||||
AuthResult _authResult;
|
||||
final fb.PhoneVerificationCompleted verificationCompleted =
|
||||
(fb.AuthCredential credential) async {
|
||||
fb.UserCredential _authResult;
|
||||
try {
|
||||
_authResult = await _fb.signInWithCredential(credential);
|
||||
print("PhoneVerificationCompleted :$_authResult");
|
||||
@@ -48,23 +48,23 @@ class AuthFb {
|
||||
'Inside _sendCodeToPhoneNumber: signInWithPhoneNumber auto succeeded: ${_authResult.user}');
|
||||
};
|
||||
|
||||
final PhoneVerificationFailed verificationFailed =
|
||||
(AuthException authException) async {
|
||||
final fb.PhoneVerificationFailed verificationFailed =
|
||||
(fb.FirebaseAuthException authException) async {
|
||||
print(
|
||||
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
|
||||
completer.completeError(SigninException(
|
||||
"Phone number verification failed:${authException.message}"));
|
||||
};
|
||||
|
||||
final PhoneCodeSent codeSent =
|
||||
(String verificationId, [int forceResendingToken]) async {
|
||||
final fb.PhoneCodeSent codeSent =
|
||||
(String verificationId, [int? forceResendingToken]) async {
|
||||
_verificationId = verificationId;
|
||||
print("codeSent " + phoneNumber);
|
||||
codeSentCompleted = true;
|
||||
completer.complete(fcs.AuthResult(authStatus: AuthStatus.SMS_SENT));
|
||||
};
|
||||
|
||||
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
|
||||
final fb.PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
|
||||
(String verificationId) {
|
||||
print("codeAutoRetrievalTimeout $verificationId ");
|
||||
|
||||
@@ -89,11 +89,11 @@ class AuthFb {
|
||||
|
||||
Future<fcs.AuthResult> signInWithPhoneNumber(String smsCode) async {
|
||||
try {
|
||||
final AuthCredential credential = PhoneAuthProvider.getCredential(
|
||||
verificationId: _verificationId,
|
||||
smsCode: smsCode,
|
||||
);
|
||||
AuthResult _authResult = await _fb.signInWithCredential(credential);
|
||||
final fb.AuthCredential credential = fb.PhoneAuthProvider.credential(
|
||||
verificationId: _verificationId, smsCode: smsCode);
|
||||
|
||||
fb.UserCredential _authResult =
|
||||
await _fb.signInWithCredential(credential);
|
||||
if (_authResult == null) {
|
||||
throw SigninException("Sigin error!");
|
||||
}
|
||||
@@ -105,19 +105,21 @@ class AuthFb {
|
||||
}
|
||||
|
||||
Future<void> signout() async {
|
||||
if (userListener != null) await userListener.cancel();
|
||||
if (userListener != null) await userListener!.cancel();
|
||||
return _fb.signOut();
|
||||
}
|
||||
|
||||
Future<void> _addUserToStream({bool refreshIdToken = false}) async {
|
||||
FirebaseUser firebaseUser = await _fb.currentUser();
|
||||
fb.User? firebaseUser = _fb.currentUser;
|
||||
if (firebaseUser == null) return null;
|
||||
Map claims = await getClaims(refreshIdToken: refreshIdToken);
|
||||
Map<dynamic, dynamic>? claims =
|
||||
await getClaims(refreshIdToken: refreshIdToken);
|
||||
|
||||
log.info("Claims:$claims");
|
||||
if (claims == null) return;
|
||||
|
||||
String cid = claims["cid"];
|
||||
User user;
|
||||
User? user;
|
||||
if (cid != null && cid != "") {
|
||||
user = await _getUserFromFirestore(cid);
|
||||
}
|
||||
@@ -136,20 +138,20 @@ class AuthFb {
|
||||
controller.add(user);
|
||||
}
|
||||
|
||||
Future<User> _getUserFromFirestore(String userID) async {
|
||||
DocumentSnapshot snap = await Firestore.instance
|
||||
Future<User?> _getUserFromFirestore(String userID) async {
|
||||
DocumentSnapshot snap = await FirebaseFirestore.instance
|
||||
.collection(user_collection)
|
||||
.document(userID)
|
||||
.doc(userID)
|
||||
.get();
|
||||
if (snap.exists) {
|
||||
User user = User.fromMap(snap.data, snap.documentID);
|
||||
User user = User.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> isLogin() async {
|
||||
final FirebaseUser firebaseUser = await _fb.currentUser();
|
||||
final fb.User? firebaseUser = _fb.currentUser;
|
||||
return Future.value(firebaseUser != null);
|
||||
}
|
||||
|
||||
@@ -193,48 +195,50 @@ class AuthFb {
|
||||
}
|
||||
|
||||
Stream<Setting> settings() async* {
|
||||
Stream<DocumentSnapshot> snapshot = Firestore.instance
|
||||
Stream<DocumentSnapshot> snapshot = FirebaseFirestore.instance
|
||||
.collection(config_collection)
|
||||
.document(setting_doc_id)
|
||||
.doc(setting_doc_id)
|
||||
.snapshots();
|
||||
|
||||
await for (var snap in snapshot) {
|
||||
Setting setting = Setting.fromMap(snap.data);
|
||||
Setting setting = Setting.fromMap(snap.data() as Map<String, dynamic>);
|
||||
yield setting;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _getCurrentUserID() async {
|
||||
FirebaseUser firebaseUser = await _fb.currentUser();
|
||||
Future<String?> _getCurrentUserID() async {
|
||||
fb.User? firebaseUser = _fb.currentUser;
|
||||
if (firebaseUser == null) return null;
|
||||
Map claims = await getClaims();
|
||||
Map? claims = await getClaims();
|
||||
if (claims == null) return null;
|
||||
String cid = claims["cid"];
|
||||
return cid;
|
||||
}
|
||||
|
||||
Future<void> _startUserListener() async {
|
||||
if (userListener != null) userListener.cancel();
|
||||
String _userID = await _getCurrentUserID();
|
||||
if (userListener != null) userListener!.cancel();
|
||||
String? _userID = await _getCurrentUserID();
|
||||
if (_userID == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Stream<DocumentSnapshot> snapshot = Firestore.instance
|
||||
Stream<DocumentSnapshot> snapshot = FirebaseFirestore.instance
|
||||
.collection(user_collection)
|
||||
.document(_userID)
|
||||
.doc(_userID)
|
||||
.snapshots();
|
||||
userListener = snapshot.listen((snap) async {
|
||||
User user = User.fromMap(snap.data, snap.documentID);
|
||||
User user = User.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
||||
|
||||
FirebaseUser firebaseUser = await _fb.currentUser();
|
||||
fb.User? firebaseUser = _fb.currentUser;
|
||||
if (firebaseUser == null) {
|
||||
userListener.cancel();
|
||||
userListener?.cancel();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// get privilege from claim
|
||||
IdTokenResult idToken = await firebaseUser.getIdToken(refresh: true);
|
||||
String privileges = idToken.claims["pr"];
|
||||
fb.IdTokenResult idToken = await firebaseUser.getIdTokenResult(true);
|
||||
|
||||
String privileges = idToken.claims?["pr"] ?? '';
|
||||
if (privileges != null && privileges != "") {
|
||||
user.privileges = privileges.split(":").toList();
|
||||
}
|
||||
@@ -245,13 +249,13 @@ class AuthFb {
|
||||
});
|
||||
}
|
||||
|
||||
StreamSubscription<DocumentSnapshot> userListener;
|
||||
Stream<User> user() {
|
||||
StreamSubscription<DocumentSnapshot>? userListener;
|
||||
Stream<User?> user() {
|
||||
// ignore: close_sinks
|
||||
StreamSubscription<FirebaseUser> authListener;
|
||||
StreamSubscription<fb.User?>? authListener;
|
||||
|
||||
Future<void> _start() async {
|
||||
authListener = _fb.onAuthStateChanged.listen((firebaseUser) async {
|
||||
authListener = _fb.authStateChanges().listen((firebaseUser) {
|
||||
if (firebaseUser == null) {
|
||||
controller.add(null);
|
||||
} else {
|
||||
@@ -263,10 +267,10 @@ class AuthFb {
|
||||
|
||||
void _stop() {
|
||||
if (userListener != null) {
|
||||
userListener.cancel();
|
||||
userListener!.cancel();
|
||||
}
|
||||
if (authListener != null) {
|
||||
authListener.cancel();
|
||||
authListener!.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/domain/constants.dart';
|
||||
@@ -36,7 +35,7 @@ class CartonDataProvider {
|
||||
}
|
||||
|
||||
Future<List<Carton>> searchCarton(String term) async {
|
||||
if (term == null || term == '') return List();
|
||||
if (term == null || term == '') return [];
|
||||
|
||||
// var bytes = utf8.encode(term);
|
||||
// var base64Str = base64.encode(bytes);
|
||||
@@ -45,7 +44,7 @@ class CartonDataProvider {
|
||||
|
||||
try {
|
||||
String path = "/$cartons_collection";
|
||||
var querySnap = await Firestore.instance
|
||||
var querySnap = await FirebaseFirestore.instance
|
||||
.collection(path)
|
||||
.where("carton_number", isEqualTo: term)
|
||||
.where("carton_type",
|
||||
@@ -53,13 +52,11 @@ class CartonDataProvider {
|
||||
.where("status", isEqualTo: carton_packed_status)
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("user_name")
|
||||
.getDocuments();
|
||||
return querySnap.documents
|
||||
.map((e) => Carton.fromMap(e.data, e.documentID))
|
||||
.toList();
|
||||
.get();
|
||||
return querySnap.docs.map((e) => Carton.fromMap(e.data(), e.id)).toList();
|
||||
} catch (e) {
|
||||
log.warning("carton error:" + e.toString());
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ class PackageDataProvider {
|
||||
token: await getToken());
|
||||
}
|
||||
|
||||
Future<List<Package>> ftsSearchPackage(String term) async {
|
||||
if (term == null || term == '') return List();
|
||||
Future<List<Package>?> ftsSearchPackage(String term) async {
|
||||
if (term == null || term == '') return [];
|
||||
|
||||
var bytes = utf8.encode(term);
|
||||
var base64Str = base64.encode(bytes);
|
||||
@@ -72,7 +72,7 @@ class PackageDataProvider {
|
||||
"/api/fts/$packages_collection/$escapePackage/$limit", "GET",
|
||||
url: Config.instance.reportURL, token: await getToken());
|
||||
|
||||
if (data == null) return List();
|
||||
if (data == null) return [];
|
||||
|
||||
data.forEach((p) {
|
||||
var package = Package.fromJson(p);
|
||||
@@ -86,7 +86,7 @@ class PackageDataProvider {
|
||||
}
|
||||
|
||||
Future<List<Package>> searchPackage(String term) async {
|
||||
if (term == null || term == '') return List();
|
||||
if (term == null || term == '') return [];
|
||||
|
||||
List<Package> packages = [];
|
||||
|
||||
|
||||
@@ -16,33 +16,33 @@ class RateDataProvider {
|
||||
static final RateDataProvider instance = RateDataProvider._();
|
||||
RateDataProvider._();
|
||||
|
||||
StreamController<Rate> controller;
|
||||
late StreamController<Rate> controller;
|
||||
static Rate _rate = Rate();
|
||||
|
||||
Stream<Rate> _rateStream() async* {
|
||||
Stream<DocumentSnapshot> snapshot = Firestore.instance
|
||||
Stream<DocumentSnapshot> snapshot = FirebaseFirestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.doc(rate_doc_id)
|
||||
.snapshots();
|
||||
await for (var snap in snapshot) {
|
||||
Rate rate = Rate.fromMap(snap.data);
|
||||
Rate rate = Rate.fromMap(snap.data() as Map<String, dynamic>);
|
||||
yield rate;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<CargoType>> _cargoTypeStream() async* {
|
||||
List<CargoType> cargoTypes = [];
|
||||
Stream<QuerySnapshot> snapshots = Firestore.instance
|
||||
Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.doc(rate_doc_id)
|
||||
.collection(cargo_types_collection)
|
||||
.where("custom_duty", isEqualTo: false)
|
||||
.snapshots();
|
||||
|
||||
await for (var snaps in snapshots) {
|
||||
cargoTypes = [];
|
||||
cargoTypes = snaps.documents.map((snap) {
|
||||
return CargoType.fromMap(snap.data, snap.documentID);
|
||||
cargoTypes = snaps.docs.map((snap) {
|
||||
return CargoType.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
||||
}).toList();
|
||||
|
||||
yield cargoTypes;
|
||||
@@ -51,17 +51,17 @@ class RateDataProvider {
|
||||
|
||||
Stream<List<CargoType>> _customDutiesStream() async* {
|
||||
List<CargoType> customDuries = [];
|
||||
Stream<QuerySnapshot> snapshots = Firestore.instance
|
||||
Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.doc(rate_doc_id)
|
||||
.collection(cargo_types_collection)
|
||||
.where("custom_duty", isEqualTo: true)
|
||||
.snapshots();
|
||||
|
||||
await for (var snaps in snapshots) {
|
||||
customDuries = [];
|
||||
customDuries = snaps.documents.map((snap) {
|
||||
return CargoType.fromMap(snap.data, snap.documentID);
|
||||
customDuries = snaps.docs.map((snap) {
|
||||
return CargoType.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
||||
}).toList();
|
||||
yield customDuries;
|
||||
}
|
||||
@@ -69,25 +69,26 @@ class RateDataProvider {
|
||||
|
||||
Stream<List<DiscountByWeight>> _discountByWeightStream() async* {
|
||||
List<DiscountByWeight> discountByWeight = [];
|
||||
Stream<QuerySnapshot> snapshots = Firestore.instance
|
||||
Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance
|
||||
.collection(config_collection)
|
||||
.document(rate_doc_id)
|
||||
.doc(rate_doc_id)
|
||||
.collection(discounts_by_weights_collection)
|
||||
.snapshots();
|
||||
|
||||
await for (var snaps in snapshots) {
|
||||
discountByWeight = [];
|
||||
discountByWeight = snaps.documents.map((snap) {
|
||||
return DiscountByWeight.fromMap(snap.data, snap.documentID);
|
||||
discountByWeight = snaps.docs.map((snap) {
|
||||
return DiscountByWeight.fromMap(
|
||||
snap.data() as Map<String, dynamic>, snap.id);
|
||||
}).toList();
|
||||
yield discountByWeight;
|
||||
}
|
||||
}
|
||||
|
||||
StreamSubscription<Rate> rateListener;
|
||||
StreamSubscription<List<CargoType>> cargoListener;
|
||||
StreamSubscription<List<CargoType>> customListener;
|
||||
StreamSubscription<List<DiscountByWeight>> discountListener;
|
||||
late StreamSubscription<Rate> rateListener;
|
||||
late StreamSubscription<List<CargoType>> cargoListener;
|
||||
late StreamSubscription<List<CargoType>> customListener;
|
||||
late StreamSubscription<List<DiscountByWeight>> discountListener;
|
||||
Stream<Rate> rate() {
|
||||
Future<void> _start() async {
|
||||
rateListener = _rateStream().listen((rate) {
|
||||
|
||||
@@ -37,23 +37,23 @@ class UserDataProvider {
|
||||
payload: {"token": token}, token: await getToken());
|
||||
}
|
||||
|
||||
Future<User> findUser(String phoneNumber) async {
|
||||
QuerySnapshot querySnap = await Firestore.instance
|
||||
Future<User?> findUser(String phoneNumber) async {
|
||||
QuerySnapshot querySnap = await FirebaseFirestore.instance
|
||||
.collection(user_collection)
|
||||
.where("phone_number", isEqualTo: phoneNumber)
|
||||
.limit(1)
|
||||
.getDocuments();
|
||||
.get();
|
||||
|
||||
if (querySnap.documents.length > 0) {
|
||||
var snap = querySnap.documents.first;
|
||||
User user = User.fromMap(snap.data, snap.documentID);
|
||||
if (querySnap.docs.length > 0) {
|
||||
var snap = querySnap.docs.first;
|
||||
User user = User.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<User>> searchUser(String term) async {
|
||||
if (term == null || term == '') return List();
|
||||
if (term == null || term == '') return [];
|
||||
|
||||
var bytes = utf8.encode(term);
|
||||
var base64Str = base64.encode(bytes);
|
||||
@@ -68,7 +68,7 @@ class UserDataProvider {
|
||||
"/api/fts/$user_collection/$escapeBuyer/$limit", "GET",
|
||||
url: Config.instance.reportURL, token: await getToken());
|
||||
|
||||
if (data == null) return List();
|
||||
if (data == null) return [];
|
||||
|
||||
data.forEach((buyer) {
|
||||
var user = User.fromJson(buyer);
|
||||
@@ -76,7 +76,7 @@ class UserDataProvider {
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("buyer error:" + e.toString());
|
||||
return null;
|
||||
// return null;
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user