[英]Flutter cubit blocprovider couldn't find the correct provider
我正在構建一個 flutter 應用程序,用戶可以在其中使用谷歌帳戶登錄。 我想用肘來管理這個。 我使用 Blocprovider 在小部件樹中提供肘部。 塊提供者位於 runApp function 中的 futurebuilder 中。 這是我的代碼:
主要的.dart:
home: FutureBuilder(
future: initRepo.initialize(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return BlocProvider(
create: (context) => SignInCubit(googleAuthRepo),
child: SignInPage(googleAuthRepo, initRepo, databaseRepo));
}
return SplashScreen();
},
));
SignInPage 小部件:
class SignInPage extends StatelessWidget {
final GoogleAuthRepo _googleAuthRepo;
final InitRepo initRepo;
final DatabaseRepo _databaseRepo;
SignInPage(this._googleAuthRepo, this.initRepo, this._databaseRepo);
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.grey[900], Colors.grey[800]])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image(image: AssetImage("lib/assets/icon.png")),
BlocBuilder(
builder: (context, state){
if(state is SignInInitial){
return buildInitial();
}
else if(state is SignInLoading){
return buildLoading();
}
else if(state is SignInLoaded){
return buildLoaded();
}
return buildInvalidUser();
},
)
],
),
);
}
Widget buildInitial() {
return GoogleButton(
this._databaseRepo, this.initRepo, this._googleAuthRepo);
}
Widget buildLoading() {
return CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(
Colors.blueGrey,
),
);
}
Widget buildInvalidUser() {
return Column(
children: [
GoogleButton(this._databaseRepo, this.initRepo, this._googleAuthRepo),
Text(
"Bejelentkezési hiba. Használj e5vos.hu-s e-mail címet!", //This is an error message, if the user doesn't use a specific email domain
style: TextStyle(color: Colors.red),
)
],
);
}
Widget buildLoaded(){
return MainPage(_databaseRepo, initRepo);
}
}
GoogleButton 小部件:
class GoogleButton extends StatefulWidget {
@override
_GoogleButtonState createState() => _GoogleButtonState();
final DatabaseRepo databaseRepo;
final InitRepo initRepo;
final GoogleAuthRepo _googleAuthRepo;
GoogleButton(this.databaseRepo, this.initRepo, this._googleAuthRepo);
}
class _GoogleButtonState extends State<GoogleButton> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
height: MediaQuery.of(context).size.height / 15,
width: MediaQuery.of(context).size.width / 7,
child: ButtonTheme(
minWidth: 300,
child: OutlineButton(
borderSide: BorderSide(color: Colors.blueGrey, width: 1),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
onPressed: () async {
final signInCubit = BlocProvider.of<SignInCubit>(context);
signInCubit.signInWithGoogle();
},
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
child: Center(
child:
Text(
'Bejelentkezés',
style: TextStyle(
fontSize: 20,
color: Colors.blueGrey,
),
),
),
)),
),
),
);
}
}
我嘗試按照它的建議進行操作,並將 BlocProvider 與 Provider 交換,所以我的代碼如下所示:
home: FutureBuilder(
future: initRepo.initialize(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Provider<SignInCubit>(
create: (context) => SignInCubit(googleAuthRepo),
builder: (context) {
return SignInPage(googleAuthRepo, initRepo, databaseRepo)
,
},);
}
return SplashScreen();
},
));
但后來我得到參數類型“SignInPage Function(BuildContext)”不能分配給參數類型“Widget Function(BuildContext, Widget)”錯誤。
我想需要查看您的 SignInPage 代碼。 目前,您似乎缺少 BlocBuilder 作為 BlocProvider 的子級。
一般來說,您的結構看起來有點 - “混合”。 為什么將 BlocProvider 用作 FutureBuilder 的子級? 我將初始化該塊,該塊依次初始化所有必要的數據,一旦准備就緒,就會產生 state 來渲染屏幕(以及在那之前的活動指示器)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.