作者:常年等奖中869 | 来源:互联网 | 2022-12-09 12:52
我们如何在StreamBuilder中添加TextField?我有一个TextField / TextFormField作为StreamBuilder或FutureBuilder的构建器函数内的小部件之一,每当我们尝试与文本字段进行交互时,它只会刷新整个构建器小部件并再次调用流/未来。
body: StreamBuilder(
stream: getClientProfile().snapshots(),
builder: (context, snapshot) {
if (snapshot.cOnnectionState== ConnectionState.active) {
print(snapshot.data.data);
Client tempClient = Client.from(snapshot.data);
print('details = ${tempClient.representative.email} ${tempClient
.address.location} ${tempClient.businessDescription}');
return Container(
child: Column(
children: [
TextFormField(
)
],
),
);
} else if (snapshot.cOnnectionState== ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
return Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.error),
),
Text('Error loading data')
],
),
);
}
}),
和消防功能
DocumentReference getClientProfile() {
return _firestore.collection(SELLERS_COLLECTION).document(_uid);
}
我想要实现的是拥有一个带有来自Firestore文档的预填充数据的表单,基本上是一个编辑表单。还有其他方法可以达到相同目的,还是我在结构上做错了什么?
编辑:
建议修改后的代码。
import 'package:flutter/material.dart';
import 'Utils/globalStore.dart';
import 'models/client_model.dart';
import 'dart:async';
class EditProfileInformation extends StatefulWidget {
@override
EditProfileInformationState createState() {
return new EditProfileInformationState();
}
}
class EditProfileInformationState extends State {
Stream dbCall;
final myCOntroller= TextEditingController();
@override
void initState() {
// TODO: implement initState
super.initState();
dbCall = getClientProfile().snapshots();
myController.addListener(_printLatestValue);
}
_printLatestValue() {
print("Second text field: ${myController.text}");
}
@override
void dispose() {
myController.removeListener(_printLatestValue);
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
// key: _scaffoldKey,
appBar: AppBar(
title: Text(
'Edit profile',
style: TextStyle(),
),
),
body: StreamBuilder(
stream: dbCall,
builder: (context, snapshot) {
if (snapshot.cOnnectionState== ConnectionState.active) {
print(snapshot.data.data);
Client tempClient = Client.from(snapshot.data);
print('details = ${tempClient.representative.email} ${tempClient
.address.location} ${tempClient.businessDescription}');
return Container(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: myController,
),
)
],
),
);
} else if (snapshot.cOnnectionState== ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
return Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.error),
),
Text('Error loading data')
],
),
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
},
child: Icon(Icons.done),
),
);
}
}
Jonah Willia..
6
为了正确使用StreamBuilder,您必须确保将使用的流缓存在State对象上。尽管StreamBuilder可以正确处理从流中获取新事件,但是接收到一个全新的Stream将强制其完全重建。就您而言,getClientProfile().snapshots()
将在调用它时创建一个全新的Stream,破坏文本字段的所有状态。
class Example extends StatefulWidget {
@override
State createState() => new ExampleState();
}
class ExampleState extends State {
Stream _stream;
@override
void initState() {
// Only create the stream once
_stream = _firestore.collection(collection).document(id);
super.initState();
}
@override
Widget build(BuildContext context) {
return new StreamBuilder(
stream: _stream,
builder: (context, snapshot) {
...
},
);
}
}
编辑:听起来还有其他问题,我无法从您提供的代码片段中诊断出来。
1> Jonah Willia..:
为了正确使用StreamBuilder,您必须确保将使用的流缓存在State对象上。尽管StreamBuilder可以正确处理从流中获取新事件,但是接收到一个全新的Stream将强制其完全重建。就您而言,getClientProfile().snapshots()
将在调用它时创建一个全新的Stream,破坏文本字段的所有状态。
class Example extends StatefulWidget {
@override
State createState() => new ExampleState();
}
class ExampleState extends State {
Stream _stream;
@override
void initState() {
// Only create the stream once
_stream = _firestore.collection(collection).document(id);
super.initState();
}
@override
Widget build(BuildContext context) {
return new StreamBuilder(
stream: _stream,
builder: (context, snapshot) {
...
},
);
}
}
编辑:听起来还有其他问题,我无法从您提供的代码片段中诊断出来。