You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Firestore集成错误:参数类型'int'无法分配给'String'类型求助

Solution for Argument Type Mismatch (int → String) in Firestore Integration

Hey there, let's get this error sorted out quickly! The core issue here is a type mismatch: your gunpla.id is an int, but the setFavoritedGunpla method expects a String for the id parameter (which makes perfect sense because Firestore document IDs are always strings).

Step 1: Fix the Immediate Error

The simplest quick fix is to convert your integer gunpla.id to a string when calling the method. Update the problematic line in gunpla_details_screen.dart like this:

context.read<Backend>().setFavoritedGunpla(
  id: gunpla.id.toString(),  // Convert int to String with toString()
  favorited: !isGunplaFavorited,
);

Step 2: Verify Backend Method Definition

Double-check your Backend class's setFavoritedGunpla method to ensure it's defined to accept a String for id. It should look something like this:

void setFavoritedGunpla({required String id, required bool favorited}) {
  // Your Firestore logic here (e.g., updating a favorites collection)
}

If you accidentally defined id as int here, that's another source of the issue—adjust it to String to match Firestore's native requirements.

If your Gunpla class's id corresponds directly to a Firestore document ID, consider changing the id field's type in the Gunpla class from int to String entirely. This eliminates the need for repeated type conversions and aligns with Firestore's behavior.

For example, update your Gunpla model:

class Gunpla {
  final String id;  // Change from int to String
  final String name;
  // ... other fields

  Gunpla({required this.id, required this.name, ...});
}

When fetching Gunpla data from Firestore, you'll now directly use the document ID string instead of parsing it to an integer.

That should resolve the argument_type_not_assignable error! Let me know if you run into any follow-up issues with the Firestore integration.

内容的提问来源于stack exchange,提问作者baselean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:32:30