29 lines
700 B
Dart
29 lines
700 B
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:momo/request/http_client.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class TokenNotifier extends Notifier<String?> {
|
|
@override
|
|
String? build() {
|
|
return "";
|
|
}
|
|
|
|
setToken(String? token) {
|
|
state = token;
|
|
dio.options.headers["Authorization"] = token;
|
|
SharedPreferences.getInstance().then((prefs) {
|
|
prefs.setString("token", token ?? "");
|
|
});
|
|
}
|
|
|
|
removeToken() {
|
|
state = '';
|
|
SharedPreferences.getInstance().then((prefs) {
|
|
prefs.setString("token", "");
|
|
});
|
|
}
|
|
}
|
|
|
|
final tokenProvider =
|
|
NotifierProvider<TokenNotifier, String?>(() => TokenNotifier());
|