Android 如何将从Home小部件获取的数据传递给it';什么是儿童小部件?
我正在为一个社区开发一个应用程序,在flutter中可以登录、注册、开会和聊天。登录成功后,路由返回主页,主页有五个底部导航。我在此应用程序中使用Firebase进行身份验证和Firestore 我想在Home组件启动时获取一次数据,并将数据传递给其他五个底部导航栏组件。 现在,每当我在导航组件之间切换时,我都会获取数据。这会增加Firestore的读数 我尝试使用构造函数变量通过组件传递数据。但这不起作用。它显示了数据无法传递到底部导航组件的错误。这是我的代码 霍姆·达特Android 如何将从Home小部件获取的数据传递给it';什么是儿童小部件?,android,flutter,google-cloud-firestore,storage,flutter-dependencies,Android,Flutter,Google Cloud Firestore,Storage,Flutter Dependencies,我正在为一个社区开发一个应用程序,在flutter中可以登录、注册、开会和聊天。登录成功后,路由返回主页,主页有五个底部导航。我在此应用程序中使用Firebase进行身份验证和Firestore 我想在Home组件启动时获取一次数据,并将数据传递给其他五个底部导航栏组件。 现在,每当我在导航组件之间切换时,我都会获取数据。这会增加Firestore的读数 我尝试使用构造函数变量通过组件传递数据。但这不起作用。它显示了数据无法传递到底部导航组件的错误。这是我的代码 霍姆·达特 class Home
class Home扩展StatefulWidget{
@凌驾
_HomeState createState()=>\u HomeState();
用户当前用户;
字符串用户标识;
主页({this.currentUser,this.userId});
}
类(HomeState扩展状态){
当前用户信息;
文档快照文档;
int _selectedIndex=0;
列出即将到来的七天;
FirestoreService_FirestoreService=FirestoreService();
静态常量文本样式选项样式=
TextStyle(fontSize:30,fontWeight:fontWeight.bold);
静态列表\u widgetOptions=[
仪表板(),
会议列表(),
事件列表(),
Chat(),
Profile(),
];
静态常量列表_appBarText=[
正文(
“仪表板”,
样式:TextStyle(
fontWeight:fontWeight.w300,
尺寸:26,
),
),
正文(
"会议",,
样式:TextStyle(
fontWeight:fontWeight.w300,
尺寸:26,
),
),
正文(
“事件”,
样式:TextStyle(fontWeight:fontWeight.w300,fontSize:26),
),
正文(
"聊天",,
样式:TextStyle(fontWeight:fontWeight.w300,fontSize:26),
),
正文(
"简介",,
样式:TextStyle(fontWeight:fontWeight.w300,fontSize:26),
),
];
@凌驾
void initState(){
//TODO:实现initState
super.initState();
//setCurrentUserID(widget.currentUser.uid);
//setCurrentUserData(doc.data());
}
void\u未映射(整数索引){
设置状态(){
_selectedIndex=索引;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:_appBarText.elementAt(_selectedIndex)),
主体:容器(
填充:边缘组。对称(水平:10),
宽度:double.maxFinite,
子项:_widgetOptions.elementAt(_selectedIndex),
),
底部导航栏:底部导航栏(
项目:常数[
底部导航气压计(
图标:图标(图标.仪表板),
标题:文本(“仪表板”),
背景颜色:颜色。黑色),
底部导航气压计(
图标:图标(Icons.people),
标题:文本(“会议”),
背景颜色:颜色。黑色),
底部导航气压计(
图标:图标(图标、日历、视图、日期),
标题:文本(“事件”),
背景颜色:颜色。黑色),
底部导航气压计(
图标:图标(Icons.chat),
标题:文本(“聊天”),
背景颜色:颜色。黑色),
底部导航气压计(
图标:图标(Icons.person),
标题:文本(“概要文件”),
背景颜色:颜色。黑色),
],
currentIndex:_selectedIndex,
selectedItemColor:Colors.lightBlue[200],
onTap:\u未映射,
标高:8.0,
背景颜色:Colors.black,
),
);
}
}
这就是每当用户切换到会议列表组件时,我从Firestore获取数据的地方。我不想那样做。相反,我希望将各自的数据从家传递到其他组件。它应该是快照,以便能够侦听更改
会议名单
class MeetingList扩展了无状态小部件{
var用户信息;
FirebaseAuth FirebaseAuth=FirebaseAuth.instance;
未来的getuserinfo()异步{
//最终uid=firebaseAuth.currentUser.uid;
//userinfo=等待firestoreService.getCurrentUserInfo(uid);
//userinfo=userinfo.data().length;
////打印(用户信息);
//返回uid;
最终uid=firebaseAuth.currentUser.uid;
DocumentSnapshot用户=等待FirebaseFirestore.instance
.collection('userProfiles')
.doc(uid)
.get();
userInfo=user.data();
返回用户信息;
}
@凌驾
小部件构建(构建上下文){
集合参考会议=
FirebaseFirestore.instance.collection('meetings');
回归未来建设者(
future:getuserinfo(),
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.waiting){
返回LoadingIndicator();
}否则{
返回流生成器(
流:会议。快照(),
建设者:
(BuildContext上下文,异步快照){
if(snapshot.hasError){
返回文本(“出错”);
}
if(snapshot.connectionState==connectionState.waiting){
返回LoadingIndicator();
}
返回新的ListView(
子项:snapshot.data.docs.map((DocumentSnapshot文档){
String meetingRole=document.data()['role'];
var userRole=userInfo['role'];
打印(用户角色);
如果(meetingRole='all'| | meetingRole==userRole){
回程会议卡(
会议:document.data(),
);
}否则{
返回容器();
}
}).toList(),
);
},
);
}
},
);
}
}
你的帮助将对com非常有帮助
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
User currentUser;
String userId;
Home({this.currentUser, this.userId});
}
class _HomeState extends State<Home> {
CurrentUser userInfo;
DocumentSnapshot doc;
int _selectedIndex = 0;
List<String> upcoming_seven_days;
FirestoreService _firestoreService = FirestoreService();
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static List<Widget> _widgetOptions = <Widget>[
Dashboard(),
MeetingList(),
EventList(),
Chat(),
Profile(),
];
static const List<Widget> _appBarText = <Widget>[
Text(
'Dashboard',
style: TextStyle(
fontWeight: FontWeight.w300,
fontSize: 26,
),
),
Text(
'Meetings',
style: TextStyle(
fontWeight: FontWeight.w300,
fontSize: 26,
),
),
Text(
'Events',
style: TextStyle(fontWeight: FontWeight.w300, fontSize: 26),
),
Text(
'Chat',
style: TextStyle(fontWeight: FontWeight.w300, fontSize: 26),
),
Text(
'Profile',
style: TextStyle(fontWeight: FontWeight.w300, fontSize: 26),
),
];
@override
void initState() {
// TODO: implement initState
super.initState();
//setCurrentUserID(widget.currentUser.uid);
//setCurrentUserData(doc.data());
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: _appBarText.elementAt(_selectedIndex)),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
width: double.maxFinite,
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.dashboard),
title: Text('Dashboard'),
backgroundColor: Colors.black),
BottomNavigationBarItem(
icon: Icon(Icons.people),
title: Text('Meetings'),
backgroundColor: Colors.black),
BottomNavigationBarItem(
icon: Icon(Icons.calendar_view_day),
title: Text('Events'),
backgroundColor: Colors.black),
BottomNavigationBarItem(
icon: Icon(Icons.chat),
title: Text('Chat'),
backgroundColor: Colors.black),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Profile'),
backgroundColor: Colors.black),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.lightBlue[200],
onTap: _onItemTapped,
elevation: 8.0,
backgroundColor: Colors.black,
),
);
}
}
class MeetingList extends StatelessWidget {
var userInfo;
FirebaseAuth firebaseAuth = FirebaseAuth.instance;
Future getuserinfo() async {
// final uid = firebaseAuth.currentUser.uid;
// userinfo = await firestoreService.getCurrentUserInfo(uid);
// userinfo = userinfo.data().length;
// //print(userinfo);
// return uid;
final uid = firebaseAuth.currentUser.uid;
DocumentSnapshot user = await FirebaseFirestore.instance
.collection('userProfiles')
.doc(uid)
.get();
userInfo = user.data();
return userInfo;
}
@override
Widget build(BuildContext context) {
CollectionReference meetings =
FirebaseFirestore.instance.collection('meetings');
return FutureBuilder(
future: getuserinfo(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return LoadingIndicator();
} else {
return StreamBuilder<QuerySnapshot>(
stream: meetings.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return LoadingIndicator();
}
return new ListView(
children: snapshot.data.docs.map((DocumentSnapshot document) {
String meetingRole = document.data()['role'];
var userRole = userInfo['role'];
print(userRole);
if (meetingRole == 'all' || meetingRole == userRole) {
return Meeting_Card(
meeting: document.data(),
);
} else {
return Container();
}
}).toList(),
);
},
);
}
},
);
}
}
class UserProvider extends ChangeNotifier {
User userInfo;
Future getuserinfo() async {
// final uid = firebaseAuth.currentUser.uid;
// userinfo = await firestoreService.getCurrentUserInfo(uid);
// userinfo = userinfo.data().length;
// //print(userinfo);
// return uid;
final uid = firebaseAuth.currentUser.uid;
DocumentSnapshot user = await FirebaseFirestore.instance
.collection('userProfiles')
.doc(uid)
.get();
userInfo = user.data();
return userInfo;
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<UserProvider>(
lazy: false,
create: (context) => UserProvider(),
child: Home(),
);
}
}
/// Get an instance of the UserProvider in the ancestors of the current widget tree like this.
UserProvider userProvider = Provider.of<UserProvider>(context);
/// Call any method inside the UserProvider class like this
userProvider.getUserInfo();
/// access any data variables inside the UserProvider class like this.
User userInfo = userProvider.userInfo;