Code Monkey home page Code Monkey logo

Comments (10)

minhdanh avatar minhdanh commented on May 23, 2024 1

I guess I figured it out. Using Typescript/Javascript, it was impossible for me to explicitly use a double type for numbers without the fractional part, like 0, 10, 123, etc. I was able to do that with Dart, that's why my normal users don't experience the problem. Furthermore the Firebase consol only shows the values as "number" is something that made it more confusing to me when there're actually 2 types of number supported: https://firebase.google.com/docs/firestore/manage-data/data-types

I had to use a Python function to do the copy. Problem fixed.

from flutterfire.

TarekkMA avatar TarekkMA commented on May 23, 2024

Hello @larssn, thank you for reporting this issue. I wasn't able to reproduce it. Here's the code I used, and it ran without errors:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  FirebaseFirestore.instance.settings = const Settings(
    persistenceEnabled: true,
  );
  if (shouldUseFirestoreEmulator) {
    FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
  }

  runApp(TheApp());
}


class TheApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              try {
                // Update operation
                FirebaseFirestore firestore = FirebaseFirestore.instance;
                DocumentReference documentReference =
                firestore.collection('test_10153').doc('counter');
                final doc = await documentReference.get();
                if (!doc.exists) {
                  await documentReference.set(<String, dynamic>{});
                }
                await documentReference.update({'counter': FieldValue.increment(1)});

                // Attempt to fetch and deserialize the updated document
                DocumentSnapshot snapshot = await documentReference.get();
                Map<dynamic, dynamic>? data = snapshot.data() as Map<dynamic, dynamic>?;
                if (data != null) {
                  // Directly deserialize 'counter' field within fromFirestore
                  int? counter = data['counter'] as int?;
                  print('Deserialized counter value: $counter');
                }
              } on Exception catch (error, stackTrace) {
                debugPrint(error.toString());
                debugPrintStack(stackTrace: stackTrace);
              }
            },
            child: Text('Increment Counter and Deserialize'),
          ),
        ),
      ),
    );
  }
}

I suggest you update all your plugins to the latest version and try again. If the issue still persists, please provide a reproduction code or repo that is failing.

from flutterfire.

larssn avatar larssn commented on May 23, 2024

They are all the latest version, and I just experienced this issue today.

And I have used FieldValue.increment without issues, but the error seems periodic.

from flutterfire.

TarekkMA avatar TarekkMA commented on May 23, 2024

What's the size of the vlaue you are trying to increment and by how much you are incrementing it?

from flutterfire.

larssn avatar larssn commented on May 23, 2024

Not much, in my tests I used about -5 to +5.
And the size incremented/subtracted, is about the same.

from flutterfire.

minhdanh avatar minhdanh commented on May 23, 2024

I'm seeing a kind of similar issue:

_TypeError: type 'int' is not a subtype of type 'double' in type cast

This happens at this line:

    final double graceAmount = snap.get('grace_amount') != null ? snap.get('grace_amount') as double : 0;

The thing is this still works fine for most of my users. Meaning snap.get('grace_amount') returns 0.0 for most of them. But yesterday when I tried to copy all the documents of a user to another, the new user encounter this issue. For this case snap.get('grace_amount') returns 0. So I guess something has changed from firestore end.

This is the firebase function that I used:

    const { sourceUserID, destUserID } = request.body;

    if (!sourceUserID || !destUserID) {
      logger.warn("Missing sourceUserID or destUserID.");
      response.sendStatus(400);
    }
    try {
      const sourceRef = getFirestore().collection("users").doc(sourceUserID);
      const destRef = getFirestore().collection("users").doc(destUserID);

      // Copy subcollections
      const sourceCollections = await sourceRef.listCollections();
      for (const collection of sourceCollections) {
        const collectionRef = sourceRef.collection(collection.id);
        const destCollectionRef = destRef.collection(collection.id);

        const collectionSnapshot = await collectionRef.get();
        for (const doc of collectionSnapshot.docs) {
          await destCollectionRef.doc(doc.id).set(doc.data(), { merge: true });
        }
      }
      logger.info("Documents copied successfully");
    } catch (error) {
      logger.error("Error copying documents");
    }

from flutterfire.

TarekkMA avatar TarekkMA commented on May 23, 2024

@minhdanh Thank you for sharing these details, appreciated.


@larssn Can you investigate if your issue is similar to the one @minhdanh mentioned? Since I couldn't get issue to happen when testing.

from flutterfire.

larssn avatar larssn commented on May 23, 2024

Ours is in pure dart, no web involved, so I doubt it's the same issue.

The "fix" was changing the int directly in firestore (away from 0 and back to 0), which caused the issue to go away.

I still think this part was weird, and is probably significant.

from flutterfire.

Tr736 avatar Tr736 commented on May 23, 2024

for me I simply us num instead of int and then num.toInt()

from flutterfire.

larssn avatar larssn commented on May 23, 2024

We use the workaround on incoming data as well, but I'd prefer we wouldn't have to.

from flutterfire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.