如何将流生成器中的数据显示到文本字段中并更新来自列表视图?
Posted
技术标签:
【中文标题】如何将流生成器中的数据显示到文本字段中并更新来自列表视图?【英文标题】:How to display data from stream builder into textfields and update the from listview? 【发布时间】:2021-02-01 05:14:54 【问题描述】:基本上,我想从 listview 更新我的数据。单击按钮编辑时,它将显示来自列表视图的数据。这是我的照片。myhomesupervisorupdateSupervisor。我已经使用了很多方法来显示,但仍然一无所获。
这是我的代码。
功能更新。
// update data supervisor
Future updateData(NewUser newUser) async
return supervisorCollection.document(newUser.id).updateData(
'name': name,
'email': email,
'nophone': nophone,
'uniqueID': uniqueID,
);
HomeSupervisor 类
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:finalyearproject/model/NewUser.dart';
import 'package:flutter/material.dart';
import 'package:finalyearproject/screen/crud/UpdateSupervisor.dart';
import 'package:finalyearproject/screen/crud/AddSupervisor.dart';
import 'package:finalyearproject/sidebar/AdminDrawer.dart';
import 'package:provider/provider.dart';
NewUser user;
class HomeSupervisor extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text('Supervisor'),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.add,
color: Colors.white,
),
onPressed: ()
Navigator.push(context, MaterialPageRoute(builder: (context) => AddSupervisor()));
)
],
),
drawer: AdminDrawer(),
body: ListSupervisor(),
);
class ListSupervisor extends StatefulWidget
@override
_ListSupervisorState createState() => _ListSupervisorState();
class _ListSupervisorState extends State<ListSupervisor>
String email;
String name;
String nophone;
String uniqueID;
@override
Widget build(BuildContext context)
return Container(
child: StreamBuilder(
stream: Firestore.instance.collection('Supervisor').snapshots(),
builder: (_, snapshot)
if (snapshot.connectionState == ConnectionState.waiting)
return Center(
child: Text("Loading..."),
);
else
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (_, index)
DocumentSnapshot sv = snapshot.data.documents[index];
return Card(
child: ListTile(
title: Container(
alignment: Alignment.centerLeft,
child: Column(
children: <Widget>[
SizedBox(height: 5.0),
Container(alignment: Alignment.centerLeft,
child: Text(sv['name']),
),
SizedBox(height: 5.0),
Container(alignment: Alignment.centerLeft,
child: Text(sv['email']),
),
SizedBox(height: 5.0),
Container(alignment: Alignment.centerLeft,
child: Text(sv['uniqueID']),
)
],
),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.delete),
color: Colors.red,
onPressed: ()
),
IconButton(
icon: Icon(Icons.edit),
color: Colors.black,
onPressed: ()
Navigator.push(context, MaterialPageRoute(builder: (context) => UpdateSupervisor()));
),
IconButton(
icon: Icon(Icons.share),
onPressed: ()
),
],
)
),
);
);
),
);
UpdateSupervisor 类
import 'package:finalyearproject/model/NewUser.dart';
import 'package:finalyearproject/service/database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class UpdateSupervisor extends StatefulWidget
final String name;
final String email;
final String uniqueID;
final String phone;
UpdateSupervisor(this.name, this.email, this.phone, this.uniqueID);
@override
_UpdateSupervisorState createState() => _UpdateSupervisorState();
class _UpdateSupervisorState extends State<UpdateSupervisor>
TextEditingController _name = new TextEditingController();
TextEditingController _email = new TextEditingController();
TextEditingController _nophone = new TextEditingController();
TextEditingController _uniqueID = new TextEditingController();
// form values
String name;
String email;
String uniqueID;
String phone;
final GlobalKey<FormState> _formKey = GlobalKey();
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text('Edit Supervisor'),
backgroundColor: Colors.redAccent,
),
body: Form(
key: _formKey,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SizedBox(height: 25.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Name',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.text,
controller: _name,
validator: (value) => value.isEmpty ? 'Name cannot be empty!': null,
onChanged: (value)
setState(() => name = value);
,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Email',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.emailAddress,
controller: _email,
validator: (value) => value.isEmpty ? 'Email cannot be empty!': null,
onChanged: (value)
setState(() => email = value);
,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Number Phone',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
controller: _nophone,
validator: (value) => value.isEmpty ? 'Number Phone cannot be empty!': null,
onChanged: (value)
setState(() => phone = value);
,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Unique ID ',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
controller: _uniqueID,
validator: (value) => value.isEmpty ? 'Ic number cannot be empty!': null,
onChanged: (value)
setState(() => uniqueID = value);
,
),
const SizedBox(height: 20.0),
RaisedButton(
color: Colors.redAccent,
textColor: Colors.black,
child: Text("Update"),
onPressed: () async
if(_formKey.currentState.validate())
DatabaseService().updateData(NewUser(name: name, email: email, nophone: phone, uniqueID: uniqueID));
_formKey.currentState.save();
),
],
),
),
)
);
我已经使用 StreamBuilder 将列表视图中的数据显示到文本字段中,但是仍然一无所获。并且还使用提供程序,但过程太慢,因为我的案例涉及许多小部件。然后现在我想尝试简单的方法但仍然一无所获。有人可以帮助我或给我任何解决这个问题的方法吗?
【问题讨论】:
您想将数据从HomeSupervisor
传递到UpdateSupervisor
吗?
没有。我想将数据从 ListSupervisor 传递给 UpdateSupervisor。
【参考方案1】:
你需要在这一行传递数据
Navigator.push(context, MaterialPageRoute(builder: (context) => UpdateSupervisor(data:sv)));
然后在你的UpdateSupervisor
:
final DocumentSnapshot sv ;
TodosScreen(Key key, @required this.sv) : super(key: key);
您可以在这里获取更多信息:Send data to a new screen
【讨论】:
但我遇到了一些错误。它说“DocumentSnapshot”类型不是“String”类型的子类型。 哦,我已经知道了。呵呵。感谢您的解决方案以上是关于如何将流生成器中的数据显示到文本字段中并更新来自列表视图?的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据库中的列字段添加到字符串生成器 c# 并实现两列布局
如何使用 AJAX 将来自 mysql 的数据显示到文本字段中