如何解决"Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update:"
How to solve "Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update:"
我的编辑class.
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!");
});
}
这是我的控制台 firebase
supervisor collection
这是我的错误
未处理的异常:PlatformException(执行更新数据时出错,NOT_FOUND:没有要更新的文档:projects/finalyearproject-5c689/databases/(默认)/documents/Supervisor/3dbac00b-29ad-4357-a480-e38a21c90042,空)
我曾尝试使用 setData() 作为我的更新函数,但它会添加新数据,而不是更新现有数据。所以有人可以帮我解决这个问题吗?有什么我遗漏的吗?
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));
我的编辑class.
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!");
});
}
这是我的控制台 firebase
supervisor collection
这是我的错误
未处理的异常:PlatformException(执行更新数据时出错,NOT_FOUND:没有要更新的文档:projects/finalyearproject-5c689/databases/(默认)/documents/Supervisor/3dbac00b-29ad-4357-a480-e38a21c90042,空)
我曾尝试使用 setData() 作为我的更新函数,但它会添加新数据,而不是更新现有数据。所以有人可以帮我解决这个问题吗?有什么我遗漏的吗?
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));