【Flutter】関数化したい。

前提

Flutterのログイン画面で
firestoreにある情報をもとに画面遷移先を分けたい。
分ける処理を関数化したときにエラーが出ている。

実現したいこと

関数のエラーをなくす。

発生している問題・エラーメッセージ

The argument type 'Future<Widget>' can't be assigned to the parameter type 'Widget?'.
The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr<Widget>', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

該当のソースコード

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return ChangeNotifierProvider<LoginModel>( create: (_) => LoginModel(), child: Consumer<LoginModel>(builder: (context, model, child) { model.checkRole(); return MaterialApp( theme: ThemeData(primarySwatch: Colors.orange), debugShowCheckedModeBanner: false, home: checkRole()); })); } } Future<Widget> checkRole() async { String? role; String? uid = SharedPrefs.fetchUid(); print(uid); final DocumentSnapshot sharedprefdocumentSnapshot = await FirebaseFirestore.instance.collection("users").doc(uid).get(); print("ドキュメントスナップショット"); print(uid); role = sharedprefdocumentSnapshot["role"]; print(role); if(role == "admin"){ const AdminPage(); }else if(role == "user"){ const UserNavigator(); } }

試したこと

materialAppが求める型にあってないみたいなので、
<Widget>を<void>に変更すると
エラーは

The argument type 'Future<void>' can't be assigned to the parameter type 'Widget?'.

だけになった

補足情報(FW/ツールのバージョンなど)

Flutter 3.0.5

コメントを投稿

0 コメント