【发布时间】:2019-12-19 01:41:35
【问题描述】:
https://felangel.github.io/bloc/#/flutterlogintutorial
我的代码在 GitHub 中: 链接-https://github.com/mymomisacoder/bloc_login2
对于本教程,我想就如何在主页(登录时)添加/显示用户名寻求建议。
预期输入:
在登录页面,输入用户名和密码后按下登录按钮。
期望的事件:
屏幕转至主页。除了主页上有注销按钮外,还显示了之前提供的用户名。
我尝试了两种方法:
方法一:在userrepo类中创建getusername()
方法2:在userrepo类中赋值,通过blocprovider访问
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final AuthenticationBloc authenticationBloc =
BlocProvider.of<AuthenticationBloc>(context);
final LoginBloc loginBloc = BlocProvider.of<LoginBloc>(context);
//method2
**//String username2 = loginBloc.usernamebloc;
//method1
String username2 = loginBloc.userRepository.getUserName().toString();**
print("$username2");
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Container(
child: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('logout'),
onPressed: () {
authenticationBloc.dispatch(LoggedOut());
},
),
Center(
child: Text("Hello"),
**//child: Text("$username2"),**
),
],
),
),
),
);
}
}
用户回购类
class UserRepository {
String username1;
Future<String> authenticate({
@required String username,
@required String password,
}) async {
await Future.delayed(Duration(seconds: 1));
//method2
username1 = username;
return 'token';
}
Future<void> deleteToken() async {
/// delete from keystore/keychain
await Future.delayed(Duration(seconds: 1));
return;
}
Future<void> persistToken(String token) async {
/// write to keystore/keychain
await Future.delayed(Duration(seconds: 1));
return;
}
Future<bool> hasToken() async {
/// read from keystore/keychain
await Future.delayed(Duration(seconds: 1));
return false;
}
//method1
**Future<String> getUserName() async {
await Future.delayed(Duration(seconds: 1));
return username1;
}**
}
主页
class SimpleBlocDelegate extends BlocDelegate {
@override
void onEvent(Bloc bloc, Object event) {
super.onEvent(bloc, event);
print(event);
}
@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
print(transition);
}
@override
void onError(Bloc bloc, Object error, StackTrace stacktrace) {
super.onError(bloc, error, stacktrace);
print(error);
}
}
void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
final userRepository = UserRepository();
runApp(
BlocProvider<AuthenticationBloc>(
builder: (context) {
return AuthenticationBloc(userRepository: userRepository)
..dispatch(AppStarted());
},
child: App(userRepository: userRepository),
),
);
}
class App extends StatelessWidget {
final UserRepository userRepository;
App({Key key, @required this.userRepository}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
bloc: BlocProvider.of<AuthenticationBloc>(context),
builder: (BuildContext context, AuthenticationState state) {
if (state is AuthenticationUninitialized) {
return SplashPage();
}
if (state is AuthenticationAuthenticated) {
return HomePage();
}
if (state is AuthenticationUnauthenticated) {
return LoginPage(userRepository: userRepository);
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
},
),
);
}
}
错误代码:
小部件库发现的异常 在构建 HomePage(dirty) 时抛出了以下断言: 使用不包含 LoginBloc 类型的 Bloc 的上下文调用 BlocProvider.of()。 从传递给的上下文开始找不到祖先 BlocProvider.of()。 在以下情况下可能会发生这种情况: 1.您使用的上下文来自 BlocProvider 上方的一个小部件。 2. 您使用了 MultiBlocProvider 并且没有明确提供 BlocProvider 类型。
好:BlocProvider(builder: (context) => LoginBloc()) 不好:BlocProvider(builder: (context) => LoginBloc())。 使用的上下文是:主页(脏)
【问题讨论】:
-
到目前为止你尝试过什么?请在此处分享代码。
-
嗨@kiner_shah,感谢您回答我的问题。我已经更新了上面的代码。我尝试了 2 种方法来访问登录表单页面中的方法;方法一:在userrepo类中创建getusername()方法二:在userrepo类中赋值,通过blocprovider访问
-
登录成功后是否打印用户名?我的意思是
HomePage类中的这一行print("$username2");。 -
您好 Kiner,这两种方法都不会打印用户名。错误代码似乎指向上下文。我已经更新了上述问题中的错误代码。
-
@kiner_shah 感谢您提供赏金。非常感谢。