New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cloud_firestore]: Double is not a subtype of int #12756
Comments
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. |
They are all the latest version, and I just experienced this issue today. And I have used |
What's the size of the vlaue you are trying to increment and by how much you are incrementing it? |
Not much, in my tests I used about -5 to +5. |
I'm seeing a kind of similar issue:
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 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");
} |
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. |
Ours is in pure dart, no web involved, so I doubt it's the same issue.
I still think this part was weird, and is probably significant. |
for me I simply us num instead of int and then num.toInt() |
We use the workaround on incoming data as well, but I'd prefer we wouldn't have to. |
Is there an existing issue for this?
Which plugins are affected?
Core
Which platforms are affected?
Android
Description
So I've just run into the issue described in #12436, and this is using
cloud_firestore_platform_interface: 6.2.2
.The "fix" was changing the int directly in firestore (away from
0
and back to0
), which caused the issue to go away.Reproducing the issue
Not sure, I'm just FieldValue.increment(ing) integers with integers.
Firebase Core version
2.30.1
Flutter Version
3.19.6
Relevant Log Output
No response
Flutter dependencies
Workspace Environment
Output from 'dart info'
/Users/lars/fvm/versions/stable/bin/cache/dart-sdk/bin/dart info
If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.
General info
Project info
Process info
Output from 'flutter doctor'
/Users/lars/fvm/versions/stable/bin/flutter doctor -v
Additional context and comments
No response
The text was updated successfully, but these errors were encountered: