如何解决由于Flutter Provider Architecture不断地进行重建,因此我的错误是什么?
我对提供商体系结构有什么误解?
我的EntryPoint:
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<AuthProvider>(
create: (context) => AuthProvider()),ChangeNotifierProvider<UserProvider>(
create: (context) => UserProvider()),],child: BdopsApp(),),);
}
class BdopsApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,theme: ThemeData(
primarySwatch: Colors.teal,visualDensity: VisualDensity.adaptivePlatformDensity,initialRoute: LoginView.routeName,routes: {
HomepageView.routeName: (context) => HomepageView(),LoginView.routeName: (context) => LoginView(),},);
}
}
class LoginView extends StatelessWidget {
static const routeName = 'LoginView';
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final User _user = User();
@override
Widget build(BuildContext context) {
return Consumer<AuthProvider>(
builder: (_,data,__) => Scaffold(
appBar: CustomAppBar.getAppBar(
title: 'BDOPS',subTitle: 'LOG IN',drawer: SidveNavDrawer(),body: Form(
key: _formKey,child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,children: [
FormInputTextBox(
labelText: 'Email',hintText: 'example@domain.com',validator: (value) {
if (value == null) {
return 'Login email not provided';
}
if (!RegExp(xEmailRegx).hasMatch(value)) {
return 'Not a valid email address';
}
},onSaved: (value) {
_user.email = value.trim();
},FormInputTextBox(
labelText: 'Password',hintText: '* * * * *',isPassword: true,validator: (value) {
if (value == null) {
return 'Login password not provided';
}
if (value.toString().length < 6) {
return 'Minmum length is six charector';
}
},onSaved: (value) {
_user.password = value.trim();
},Divider(
height: 30.0,RaisedButton(
color: Colors.deepOrange,padding: EdgeInsets.symmetric(vertical: 10.0),onpressed: () async {
if (!_formKey.currentState.validate()) {
return;
}
_formKey.currentState.save();
var res = await data.loginUser(_user);
if (res == true) {
AlertDialog(
title: Text('Login Success'),);
} else {
AlertDialog(
title: Text('Login Failed'),);
}
Navigator.pushReplacementNamed(
context,HomepageView.routeName);
},child: Text(
'SUBMIT',style: xHeaderText,);
}
}
然后,我导航到主页,在页面加载时,屏幕应从AuthUser Provider的getAuthUser中获取Auth用户,并自行触发一种方法,该方法可使用userID从API中获取登录的用户详细信息。
class HomepageView extends StatelessWidget {
static const routeName = 'HomePageView';
@override
Widget build(BuildContext context) {
final userId = Provider.of<AuthProvider>(context).getAuthUser.id;
return Consumer<UserProvider>(
builder: (_,dataProvider,__) {
dataProvider.fetchAUser(userId);
return Scaffold(
appBar: CustomAppBar.getAppBar(title: 'BDOPS',subTitle: 'Home'),body: Text(dataProvider.getSingelUser.email),);
},);
}
}
这样做,我的应用程序首先抛出一个错误,然后不断重建并一次又一次地调用API。
我的提供商类别为:
class AuthProvider with ChangeNotifier {
User _authUser;
String _errorMessage;
AuthTokens _tokens;
Future<bool> loginUser(User user) async {
if (user.email == null || user.password == null) {
_setErrorMessage('Provide login credentials');
}
var resp = await APIService().loginUser(
email: user.email,password: user.password,);
if (resp.statusCode == 200) {
_setAuthToken(authTokensFromJson(resp.body));
var userFetched =
await UserProvider().fetchAUser(decodeJwt(_tokens.access)['user_id']);
if (userFetched != null) {
_setAuthUser(userFetched);
return true;
}
}
_setErrorMessage('Failed to login');
return false;
}
void _setAuthToken(value) {
_tokens = value;
}
void _setAuthUser(value) {
_authUser = value;
notifyListeners();
}
User get getAuthUser {
return _authUser;
}
void _setErrorMessage(value) {
_errorMessage = value;
notifyListeners();
}
String get getErrorMessage {
return _errorMessage;
}
}
class UserProvider with ChangeNotifier {
User _user;
Future<User> fetchAUser(userId) async {
var response = await APIService().getAUser(userId: userId);
if (response.statusCode == 200) {
setUser(userFromJson(response.body));
print('Called from UserProvider');
return _user;
}
return null;
}
void setUser(value) {
_user = value;
notifyListeners();
}
User get getSingelUser {
return _user;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。