x1c 0d1x我正在用flutter开发一个应用程序。应用程序应该为不同类型的用户使用不同的页面。有4种用户:“管理员”,“职员”,“会员”,“收藏家”。问题是,我无法到达不同的页面,并始终停留在SiginPage。我是新的这扑和 dart ,所以我需要有人来帮助我为我的顶点项目。谢谢你!
这是我的main代码dart
import 'package:capstone_project/account/admin_page.dart';
import 'package:capstone_project/account/collector_page.dart';
import 'package:capstone_project/account/member_page.dart';
import 'package:capstone_project/account/staff_page.dart';
import 'package:capstone_project/signinsignuppage/signin_page.dart';
import 'package:capstone_project/signinsignuppage/utils.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
final navigatorKey = GlobalKey<NavigatorState>();
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
scaffoldMessengerKey: messengerKey,
debugShowCheckedModeBanner: false,
home: const MainPage(),
);
}
}
class MainPage extends StatelessWidget {
const MainPage({Key? key}) : super(key: key);
Future<String> getRole(String uid) async {
String role = 'member';
try {
final snapshot = await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.get();
if (snapshot.exists) {
role = snapshot.get('role').toString();
} else {
print('Document does not exist in the database');
}
} catch (e) {
print(e);
}
return role;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return const Center(child: Text('Something Went Wrong!'));
} else if (snapshot.hasData) {
// User is authenticated, check their role
final user = snapshot.data!;
return FutureBuilder<String>(
future: getRole(user.uid),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return const Center(child: Text('Something Went Wrong!'));
} else {
final String? role = snapshot.data;
if (role == "admin") {
return const AdminPage();
} else if (role == "staff") {
return const StaffPage();
} else if (role == "member") {
return const MemberPage();
} else if (role == "collector") {
return const CollectorPage();
} else {
// Unknown role, handle accordingly
return const SigninPage();
}
}
},
);
} else {
// User is not authenticated, show sign-in page
return const SigninPage();
}
},
),
);
}
}
下面是我在Sign Up中的代码:
Future signUp() async{
final isValid = formKey.currentState!.validate();
if (!isValid) return;
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => const Center(child: CircularProgressIndicator()));
try{
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
//Display User to FireStore
addUserDetailes(
firstnameController.text.trim(),
middlenameController.text.trim(),
lastnameController.text.trim(),
fulladdressController.text.trim(),
civilstatuscontroller.text.trim(),
emailController.text.trim(),
int.parse(phonenoController.text.trim()),
int.parse(ageController.text.trim()),
);
}on FirebaseAuthException catch (e){
print(e);
Utils.showSnackBar(e.message);
}
//Navigator.of(context) not working!
navigatorKey.currentState!.popUntil((route) =>route.isFirst);
}
//display to database
Future addUserDetailes(String firstName, String middleName, String lastName, String fullAddress, String civilStatus, String email, int phoneNumber, int age ) async{
await FirebaseFirestore.instance.collection('users').add({
'first name': firstName,
'middle name': middleName,
'last name': lastName,
'full address': fullAddress,
'civil status' : civilStatus,
'email': email,
'phone number': phoneNumber,
'age' : age,
});
}
下面是getRole的代码:
Future<String> getRole(String id) async {
String role = "member";
try {
final snapshot = await FirebaseFirestore.instance
.collection(FireStoreConstants.users)
.doc(id)
.get();
if (snapshot.exists) {
print('Document data: ${snapshot.data()}');
role = snapshot.get("role").toString();
} else {
print('Document does not exist in the database');
}
} catch (e) {
print(e);
}
return role;
}
}
class FireStoreConstants {
static const String users = 'users';
}
但代码仍然指向return const SigninPage();
2条答案
按热度按时间wgmfuz8q1#
在你的getRole函数中,你需要这样使用,这是返回String,所以你可能需要检查UserRole类型是否等于String,如果角色没有成功获取,这将返回成员
}
之后在你的未来构建器中,你没有得到UserRole,而是得到String,将模型更改为String,
cyej8jka2#
在构建函数中,将
return FutureBuilder<UserRole>
替换为returnFutureBuilder<String>
如果这不能解决您的问题,那么在构建函数中执行
print(snapshot.data);
以进行调试。