class UserModel extends ChangeNotifier{
User _user;
set setuser(User user)
{
_user = user;
notifyListeners();
}
User get getuser{
return _user;
}
}
class UserLoader extends StatelessWidget
{
final _db= FireStoreService();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('HomeScreen'),
),
body: FutureBuilder(
future: _db.getUserDetails()
builder:(context,futureData){
if(futureData.state == ConnectionState.done)
{
if(futureData.hasData)
{
Provider.of<UserModel>.setuser = futureData.data;
}
}
}
),
);
}
}
我知道这是因为 Provider.of.setuser = futureData.data; 因为它在构建函数正在进行时再次调用构建函数。我有任何替代方法可以在 Provider 中设置值,但在使用构建器的情况下不专门构建它。