我的编辑班。
import 'package:finalyearproject/model/NewUser.dart';
import 'package:finalyearproject/service/database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';
class UpdateSupervisor extends StatefulWidget {
final NewUser sv;
UpdateSupervisor({Key key, this.sv}) : super(key: key);
@override
_UpdateSupervisorState createState() => _UpdateSupervisorState();
}
class _UpdateSupervisorState extends State<UpdateSupervisor> {
final GlobalKey<FormState> _formKey = GlobalKey();
TextEditingController _name;
TextEditingController _email;
TextEditingController _nophone;
TextEditingController _uniqueID;
var uuid = Uuid();
@override
void initState(){
super.initState();
_name = TextEditingController(text: widget.sv.name);
_email = TextEditingController(text: widget.sv.email);
_nophone = TextEditingController(text: widget.sv.nophone);
_uniqueID= TextEditingController(text: widget.sv.uniqueID);
}
@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,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Email',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.emailAddress,
controller: _email,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Number Phone',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
controller: _nophone,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Unique ID ',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
controller: _uniqueID,
),
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.text, email: _email.text, nophone: _nophone.text, uniqueID: _uniqueID.text, id: uuid.v4()));
}
}
),
],
),
),
)
);
}
}
我的功能更新
// update data supervisor
Future updateData(NewUser newUser) async {
return supervisorCollection.document(newUser.id).updateData(
newUser.toJson()).whenComplete(() {
print("Update success!");
});
}
这是我的控制台火力发源地
这是我的错误
未处理的异常:PlatformException(执行updateData时出错,NOT_FOUND:没有要更新的文档:projects / finalyearproject-5c689 / databases /(默认)/ documents / Supervisor / 3dbac00b-29ad-4357-a480-e38a21c90042,空)
我尝试将setData()用于我的更新函数,但它将添加新数据,而不更新现有数据。那么有人可以帮助我解决这个问题吗?我有什么想念的吗?
答案 0 :(得分:0)
uuid.v4()将生成一个v4(随机)ID,并且您正尝试使用此新随机生成的ID(您的集合中不存在)来更新文档。
要修复它:
var id = widget.sv.id;
在新用户中使用此ID发送更新功能:
DatabaseService().updateData(NewUser(name: _name.text, email: _email.text, nophone: _nophone.text, uniqueID: _uniqueID.text, id: id));