Flutter streambuilder 不会更新 statefulwidget 或 statelesswidget
Flutter streambuilder does not update a statefulwidget or statelesswidget
我正在尝试构建一个 streambuilder。
我的问题是 streambuilder 不更新 streambuilder 小部件内的其他 statefulWidgets。
这是我的代码:
class ChatPage extends StatefulWidget {
//pass parm
var chat_object;
ChatPage(this.chat_object, {Key? key}) : super(key: key);
@override
_ChatPage createState() => _ChatPage();
}
class _ChatPage extends State<ChatPage> {
//pass parm
var chat_object;
_ChatPage(this.chat_object);
@override
Widget build(BuildContext context) {
// Full screen width and height
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
// Height (without SafeArea)
var padding = MediaQuery.of(context).padding;
double height1 = height - padding.top - padding.bottom;
// Height (without status bar)
double height2 = height - padding.top;
// Height (without status and toolbar)
double height3 = height - padding.top - kToolbarHeight;
//scaffold
return Scaffold(
appBar: customAppBarGoBack('Chat', context),
body: LimitedBox(
maxHeight: height3,
child: Stack(
children: [
StreamBuilder<dynamic>(
stream: FirebaseFirestore.instance
.collection('chat')
.doc('chat_messages')
.collection(chat_object.chat_message_id[0].trim())
.orderBy('message_time')
.snapshots(),
builder: (
BuildContext context,
AsyncSnapshot<dynamic> snapshot,
) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
if (snapshot.hasError) {
return const Text('Error');
} else if (snapshot.hasData) {
//chat room object list
var chat_room_object =
load_object(snapshot.data, chat_object).first;
//chat message object list
var message_object =
load_object(snapshot.data, chat_object).last;
print('message_object: ' + message_object.toString());
return Test(chat_room_object, message_object);
} else {
return const Text('Empty data');
}
}
},
),
],
),
),
drawer: CustomSideBar(context),
);
}
}
//test
class Test extends StatefulWidget {
//pass parm
var chat_room_object;
var message_object;
Test(this.chat_room_object, this.message_object, {Key? key})
: super(key: key);
@override
_Test createState() => _Test(chat_room_object, message_object);
}
class _Test extends State<Test> {
//pass parm
_Test(this.chat_room_object, this.message_object);
var chat_room_object;
var message_object;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: message_object.length,
itemBuilder: (context, i) {
return Text(message_object[i].message.toString());
},
);
}
}
如果我在 streambuilder 中构建列表,列表 将被更新。
但是,如果我在一个新的小部件中构建列表 并将值传递给列表,则该小部件将不会更新 .
任何人都可以给我一个简单示例我如何更新 streambuilder 中包含的 statefulwidget 中的值?
您可以尝试直接从 widget.Example 访问它们,而不是在 Test 小部件状态的构造函数中传递参数,如下所示。
//test
class Test extends StatefulWidget {
//pass parm
var chat_room_object;
var message_object;
Test(this.chat_room_object, this.message_object, {Key? key})
: super(key: key);
@override
_Test createState() => _Test();
}
class _Test extends State<Test> {
//pass parm
_Test();
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.message_object.length,
itemBuilder: (context, i) {
return Text(widget.message_object[i].message.toString());
},
);
}
}
我正在尝试构建一个 streambuilder。
我的问题是 streambuilder 不更新 streambuilder 小部件内的其他 statefulWidgets。
这是我的代码:
class ChatPage extends StatefulWidget {
//pass parm
var chat_object;
ChatPage(this.chat_object, {Key? key}) : super(key: key);
@override
_ChatPage createState() => _ChatPage();
}
class _ChatPage extends State<ChatPage> {
//pass parm
var chat_object;
_ChatPage(this.chat_object);
@override
Widget build(BuildContext context) {
// Full screen width and height
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
// Height (without SafeArea)
var padding = MediaQuery.of(context).padding;
double height1 = height - padding.top - padding.bottom;
// Height (without status bar)
double height2 = height - padding.top;
// Height (without status and toolbar)
double height3 = height - padding.top - kToolbarHeight;
//scaffold
return Scaffold(
appBar: customAppBarGoBack('Chat', context),
body: LimitedBox(
maxHeight: height3,
child: Stack(
children: [
StreamBuilder<dynamic>(
stream: FirebaseFirestore.instance
.collection('chat')
.doc('chat_messages')
.collection(chat_object.chat_message_id[0].trim())
.orderBy('message_time')
.snapshots(),
builder: (
BuildContext context,
AsyncSnapshot<dynamic> snapshot,
) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else {
if (snapshot.hasError) {
return const Text('Error');
} else if (snapshot.hasData) {
//chat room object list
var chat_room_object =
load_object(snapshot.data, chat_object).first;
//chat message object list
var message_object =
load_object(snapshot.data, chat_object).last;
print('message_object: ' + message_object.toString());
return Test(chat_room_object, message_object);
} else {
return const Text('Empty data');
}
}
},
),
],
),
),
drawer: CustomSideBar(context),
);
}
}
//test
class Test extends StatefulWidget {
//pass parm
var chat_room_object;
var message_object;
Test(this.chat_room_object, this.message_object, {Key? key})
: super(key: key);
@override
_Test createState() => _Test(chat_room_object, message_object);
}
class _Test extends State<Test> {
//pass parm
_Test(this.chat_room_object, this.message_object);
var chat_room_object;
var message_object;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: message_object.length,
itemBuilder: (context, i) {
return Text(message_object[i].message.toString());
},
);
}
}
如果我在 streambuilder 中构建列表,列表 将被更新。 但是,如果我在一个新的小部件中构建列表 并将值传递给列表,则该小部件将不会更新 .
任何人都可以给我一个简单示例我如何更新 streambuilder 中包含的 statefulwidget 中的值?
您可以尝试直接从 widget.Example 访问它们,而不是在 Test 小部件状态的构造函数中传递参数,如下所示。
//test
class Test extends StatefulWidget {
//pass parm
var chat_room_object;
var message_object;
Test(this.chat_room_object, this.message_object, {Key? key})
: super(key: key);
@override
_Test createState() => _Test();
}
class _Test extends State<Test> {
//pass parm
_Test();
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.message_object.length,
itemBuilder: (context, i) {
return Text(widget.message_object[i].message.toString());
},
);
}
}