vansika
vansika

Reputation: 75

Unhandled Exception: [cloud_firestore/not-found] Some requested document was not found

In my flutter application, this is the error thats coming, The issue that im facing is after i click on the address line (shown in the following image) issue picture we enter a map screen, which is the following image issue img in order to set the delivery location, but then after i press "continue" the following error pops up,

W/Firestore(21269): (23.0.3) [WriteStream]: (23ef168) Stream closed with status: Status{code=NOT_FOUND, description=No document to update: projects/grocery-app-d1d06/databases/(default)/documents/users/ZwfXvwLAKP0g7Kbnb1k2, cause=null}.
E/flutter (21269): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: [cloud_firestore/not-found] Some requested document was not found.
E/flutter (21269): #0      MethodChannelDocumentReference.update (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_reference.dart:60:7)
E/flutter (21269): <asynchronous suspension>
E/flutter (21269): #1      UserServices.updateUserData (package:groceryapp/services/user_services.dart:16:5)
E/flutter (21269): <asynchronous suspension>

Following is my user services code:

// @dart=2.9
import 'package:cloud_firestore/cloud_firestore.dart';

class UserServices{

  String collection ='users';
  FirebaseFirestore _firestore = FirebaseFirestore.instance;

  Future<void> createUserData(Map<String, dynamic> values)async{
    String id= values['id'];
    await _firestore.collection(collection).doc(id).set(values);
  }

  Future<void> updateUserData (Map<String, dynamic>values)async{
    String id = values['id'];
    await _firestore.collection(collection).doc(id).update(values);
  }

  Future<DocumentSnapshot>getUserById(String id) async{
    var result = await _firestore.collection(collection).doc(id).get();

      return result;
  }
}

and following is my mapscreen code

Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: SizedBox(
                      width: MediaQuery.of(context).size.width-40,
                      child: AbsorbPointer(
                        absorbing: _locating ? true : false,
                        child: TextButton(
                          style:TextButton.styleFrom(
                            backgroundColor:_locating ?
                            Colors.grey: Theme.of(context).primaryColor,
                          ),
                          onPressed:(){
                            locationData.savePrefs();

                            if(_loggedIn==false){
                              Navigator.pushNamed(context, LoginScreen.id);
                            }
                            else{
                              setState((){
                                _auth.latitude= locationData.latitude;
                                _auth.longitude= locationData.longitude;
                                _auth.address=locationData.selectedAddress.addressLine;
                              });
                              _auth.updateUser(
                                id: user?.uid,
                                number: user?.phoneNumber,
                              );
                            }
                          },
                          child: Text(
                            'CONFIRM LOCATION',
                            style: TextStyle(
                                color: Colors.white,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),

and following is my appBarcode

class _MyAppBarState extends State<MyAppBar> {
  String _location= '';
  String _address= '';

  @override
  void initState() {
    getPrefs();
    super.initState();
  }

  getPrefs()async{
    SharedPreferences prefs= await SharedPreferences.getInstance();
    String location= prefs.getString('location');
    String address= prefs.getString('address');
    setState((){
      _location= location;
      _address= address;
    });
  }

  @override
  Widget build(BuildContext context) {
    final locationData =Provider.of<LocationProvider>(context);
    return AppBar(
      automaticallyImplyLeading: true,
      elevation: 0.0,
      title: TextButton(
        onPressed:(){
          locationData.getCurrentPosition();
          if(locationData.permissionAllowed==true){
            Navigator.pushNamed(context, MapScreen.id);
          }
          else{
            print('Permission not Allowed');
          }
        },
        child:Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
            children:[
              Flexible(
                child: Text(
                  _location == null? 'Address not set' : _location,
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              Icon(
                Icons.edit_outlined,
                color: Colors.white,
                size:15,
              ),
            ],
          ),
            Flexible(child: Text(
              _address,
              overflow:TextOverflow.ellipsis,
              style:TextStyle(
                color:Colors.white,
                fontSize:12,
              ),
            ),
            ),
        ],
        ),
      ),

Upvotes: 1

Views: 2616

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

It looks like you're calling update on a reference to a non-existing document, which is not allowed in Cloud Firestore.

If you want to either create the document if it doesn't exist yet, or update the document if is does exist already, you can use SetOptions(merge: true) with set() like this:

Future<void> updateUserData (Map<String, dynamic>values)async{
  String id = values['id'];
  await _firestore.collection(collection).doc(id).set(values, SetOptions(merge: true));
}

Also see the FlutterFire documentation on adding documents.

Upvotes: 4

Related Questions