如何将流生成器中的数据显示到文本字段中并更新列表视图?

How to display data from stream builder into textfields and update the from listview?

基本上,我想从列表视图更新我的数据。单击按钮编辑时,它将显示列表视图中的数据。这是我的照片。myhomesupervisor updateSupervisor。我已经用了很多方法显示了,还是没有显示。

这是我的代码。

功能更新。

 // update data supervisor
    Future updateData(NewUser newUser) async {
    return supervisorCollection.document(newUser.id).updateData({
        'name': name,
        'email': email,
        'nophone': nophone,
        'uniqueID': uniqueID,
    });
    }

家庭主管class

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: () {

                                  }
                              ),
                            ],
                          )
                      ),
                    );
                  });
            }
          }),
    );
  }
}

更新主管class

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 将列表视图中的数据显示到文本字段中,但是仍然一无所获。并且还使用了提供者,但是过程太慢了,因为我的案例涉及很多小部件。然后现在我想尝试简单的方法,但仍然一无所获。有人可以帮助我或给我任何解决方案来解决这个问题吗?

你需要在这一行传递数据

 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