我在flutter中使用StreamBuilder widget时遇到了问题。我试图访问一个存储令牌的自定义类。
class User {
String token;
User({this.token});
}
===============================
class AuthService {
String url = 'https://reqres.in/api/login';
String token = '';
// {
// "email": "eve.holt@reqres.in",
// "password": "cityslicka"
// }
Map data;
Future signIn(String email, String password) async {
final response =
await post(url, body: {'email': email, 'password': password});
data = jsonDecode(response.body);
print(data['token']);
token = data['token'];
_userFromDatabaseUser(data);
return data;
}
//create user obj based on the database user
User _userFromDatabaseUser(Map user) {
return user != null ? User(token: user['token']) : null;
}
//user stream for provider
Stream<User> get user {
return .................. ;
}
Stream.value
从你的一个值创建一个流,但如果你的数据本身不是流,那又有什么意义呢?你想让你的流代表什么? - nvoigtcode
class Wrapper extends StatelessWidget { @override Widget build(BuildContext context) { final user = Provider.of<User>(context); print(user); if (user == null) { return Authenticate(); } else { return Home(); } } }code
- James Maddinson