将 int 数据添加到 Firestore
Add int data to Firestore
我开始使用 firebase 和 flutter,我正在尝试将数据添加到 Firestore。我在添加字符串时成功了,但在尝试添加整数(例如年龄)时遇到了问题。我知道我可以将年龄用作字符串并将键盘设置为仅数字,但我很好奇为什么它不起作用。
代码如下:
class _HomePageState extends State<HomePage> {
String name = '';
int age = 0;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
decoration: InputDecoration(hintText: 'name'),
onChanged: (value) {
name = value;
;
},
),
TextField(
decoration: InputDecoration(hintText: 'age'),
onChanged: (value) {
age = value as int;
},
),
SizedBox(height: 20),
ElevatedButton(
child: Text('add info into firestore'),
onPressed: () {
FirebaseFirestore.instance.collection('collectionPath').add({
'name': '$name',
'age': age,
});
},
)
],
),
),
),
);
}
}
在调试中,我得到:
Exception caught by widgets ═══════════════════════════════════════════
type 'String' is not a subtype of type 'int' in type cast
在您的 collection 内部,您必须制作一个称为文档的东西。该文档允许包含字符串、整数、布尔值、列表、映射等内容。目前您只能存储字符串的原因是您的代码隐式尝试创建一个名为 $name 的新文档变量,作为旁注,文档名称只能是字符串,这就是为什么它会忽略您的年龄整数。
为了解决您的问题,您的 onPressed() 函数应该如下所示:
onPressed: () {
FirebaseFirestore.instance.collection('collectionPath').doc('documentName').set({
'name' : '$name',
'age' : age
});
}
在名为 'collectionPath' 的 Collection 中,此代码将创建一个名为 'documentName' 的文档(您可以将其更改为您想要的任何内容),并在该文档中设置姓名和年龄变量。在Document上调用了set()方法,它的参数是一个Map,由你要存储的变量组成。
如果您有任何问题,请告诉我。
您收到错误是因为您试图将 String
转换为 int
。您应该 parse 将 String
改为 int
.
引用 The Dart type system | Dart 中的一段话,深入了解为什么类型转换在运行时失败:
Dart’s type system, like the type systems in Java and C#, is sound. It
enforces that soundness using a combination of static checking
(compile-time errors) and runtime checks. For example, assigning a
String to int is a compile-time error. Casting an object to a String
using as String fails with a runtime error if the object isn’t a
String.
解决方法:
在年龄 TextField's
onChanged
方法中更改此行:
age = value as int;
对此:
age = int.parse(value);
我开始使用 firebase 和 flutter,我正在尝试将数据添加到 Firestore。我在添加字符串时成功了,但在尝试添加整数(例如年龄)时遇到了问题。我知道我可以将年龄用作字符串并将键盘设置为仅数字,但我很好奇为什么它不起作用。
代码如下:
class _HomePageState extends State<HomePage> {
String name = '';
int age = 0;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
decoration: InputDecoration(hintText: 'name'),
onChanged: (value) {
name = value;
;
},
),
TextField(
decoration: InputDecoration(hintText: 'age'),
onChanged: (value) {
age = value as int;
},
),
SizedBox(height: 20),
ElevatedButton(
child: Text('add info into firestore'),
onPressed: () {
FirebaseFirestore.instance.collection('collectionPath').add({
'name': '$name',
'age': age,
});
},
)
],
),
),
),
);
}
}
在调试中,我得到:
Exception caught by widgets ═══════════════════════════════════════════ type 'String' is not a subtype of type 'int' in type cast
在您的 collection 内部,您必须制作一个称为文档的东西。该文档允许包含字符串、整数、布尔值、列表、映射等内容。目前您只能存储字符串的原因是您的代码隐式尝试创建一个名为 $name 的新文档变量,作为旁注,文档名称只能是字符串,这就是为什么它会忽略您的年龄整数。
为了解决您的问题,您的 onPressed() 函数应该如下所示:
onPressed: () {
FirebaseFirestore.instance.collection('collectionPath').doc('documentName').set({
'name' : '$name',
'age' : age
});
}
在名为 'collectionPath' 的 Collection 中,此代码将创建一个名为 'documentName' 的文档(您可以将其更改为您想要的任何内容),并在该文档中设置姓名和年龄变量。在Document上调用了set()方法,它的参数是一个Map,由你要存储的变量组成。
如果您有任何问题,请告诉我。
您收到错误是因为您试图将 String
转换为 int
。您应该 parse 将 String
改为 int
.
引用 The Dart type system | Dart 中的一段话,深入了解为什么类型转换在运行时失败:
Dart’s type system, like the type systems in Java and C#, is sound. It enforces that soundness using a combination of static checking (compile-time errors) and runtime checks. For example, assigning a String to int is a compile-time error. Casting an object to a String using as String fails with a runtime error if the object isn’t a String.
解决方法:
在年龄 TextField's
onChanged
方法中更改此行:
age = value as int;
对此:
age = int.parse(value);