![](/img/trans.png)
[英]How to do POST API in flutter using Dio ,retrofit and riverpod?
[英]Flutter api login using riverpod
我正在嘗試使用 Riverpod 通過 laravel 后端登錄。 現在我只是從存儲庫中返回 true 或 false。 我設置了一個接受 email 和密碼的表單。 isLoading變量只是為了顯示一個圓形指示器。 我已經運行了代碼並且它可以工作,但不確定我是否正確使用了riverpod。 有更好的方法嗎?
auth_provider.dart
class Auth{
final bool isLogin;
Auth(this.isLogin);
}
class AuthNotifier extends StateNotifier<Auth>{
AuthNotifier() : super(Auth(false));
void isLogin(bool data){
state = new Auth(data);
}
}
final authProvider = StateNotifierProvider((ref) => new AuthNotifier());
auth_repository.dart
class AuthRepository{
static String url = "http://10.0.2.2:8000/api/";
final Dio _dio = Dio();
Future<bool> login(data) async {
try {
Response response = await _dio.post(url+'sanctum/token',data:json.encode(data));
return true;
} catch (error) {
return false;
}
}
}
login_screen.dart
void login() async{
if(formKey.currentState.validate()){
setState((){this.isLoading = true;});
var data = {
'email':this.email,
'password':this.password,
'device_name':'mobile_phone'
};
var result = await AuthRepository().login(data);
if(result){
context.read(authProvider).isLogin(true);
setState((){this.isLoading = false;});
}else
setState((){this.isLoading = false;});
}
}
由於我不是來自移動背景並且最近在我最近的項目中使用了flutter+riverpod,我不能說這是最佳實踐。 但有幾點我想說明:
IAuthRepository
作為存儲庫。 Riverpod 可以充當依賴注入。final authRepository = Provider<IAuthRepository>((ref) => AuthRepository());
Future<bool> login(String email, String password) async {
try {
var data = {
'email': email,
'password': password,
'device_name':'mobile_phone'
};
Response response = await _dio.post(url+'sanctum/token',data:json.encode(data));
return true;
} catch (error) {
return false;
}
}
class AuthNotifier extends StateNotifier<Auth>{
final ProviderReference ref;
IAuthRepository _authRepository;
AuthNotifier(this.ref) : super(Auth(false)) {
_authRepository = ref.watch(authRepository);
}
Future<void> login(String email, String password) async {
final loginResult = await_authRepository.login(email, password);
state = Auth(loginResult);
}
}
final authProvider = StateNotifierProvider((ref) => new AuthNotifier(ref));
login
方法login() {
context.read(authProvider).login(this.email, this.password);
}
Auth
。 至少,我通常會創建一個抽象的BaseAuthState
,它派生為AuthInitialState
、 AuthLoadingState
、 AuthLoginState
、 AuthErrorState
等。class AuthNotifier extends StateNotifier<BaseAuthState>{
...
AuthNotifier(this.ref) : super(AuthInitialState()) { ... }
...
}
Consumer(builder: (context, watch, child) {
final state = watch(authProvider.state);
if (state is AuthLoginState) ...
else if (state is AuthLoadingState) ...
...
})
我喜歡使用枚舉或 class 來驗證 state 而不是使用布爾值
enum AuthState { initialize, authenticated, unauthenticated }
並用於登錄 state
enum LoginStatus { initialize, loading, success, failed }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.