【发布时间】:2023-01-28 10:03:52
【问题描述】:
import 'package:demo_app/services/api.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AuthProvider extends ChangeNotifier{
bool isAuthenticated = false;
late String token;
late ApiService apiService;
AuthProvider() {
init();
}
Future<void> init() async {
token = await getToken();
if (token.isNotEmpty) {
isAuthenticated = true;
}
apiService = ApiService(token);
notifyListeners();
}
Future<void> register(String name, String email, String password, String passwordConfirm, String deviceName) async{
token = await apiService.register(name, email, password, passwordConfirm, deviceName);
isAuthenticated = true;
setToken();
notifyListeners();
}
Future<void> logIn(String email, String password, String deviceName) async{
token = await apiService.login(email, password, deviceName);
isAuthenticated = true;
setToken();
notifyListeners();
}
Future<void> logOut() async{
token = '';
isAuthenticated = false;
setToken();
notifyListeners();
}
Future<void> setToken() async{
final pref = await SharedPreferences.getInstance();
pref.setString('token', token);
}
Future<void> getToken() async{
final pref = await SharedPreferences.getInstance();
pref.getString('token') ?? '';
}
}
令牌 = 等待 getToken();
给出这个错误
此表达式的类型为“void”,因此无法使用其值。 尝试检查您是否使用了正确的 API;可能有一个函数或调用返回了您没有预料到的 void。还要检查也可能为空的类型参数和变量。
关于解决这个问题的任何线索?
【问题讨论】:
-
getToken返回Future<void>。它可能应该返回Future<String>。 -
哪条线导致了这个问题?
-
令牌 = 等待 getToken();