Flutter FutureBuilder 类型 'String' 不是 'index' 的 'int' 类型的子类型



我正试图使用futurebuilder从firebase firestore获取我的文档,但我面临的是this type 'String' is not a subtype of type 'int' of 'index'错误。firestore中的值为string。我不知道在哪里迭代或者使用了int。我试过添加。tostring,但没有好的结果。如果有人知道我在哪里犯了错误,那么请纠正,我也用这种方法来更新个人资料,让我知道它是好的还是我做错了。注:这是我的第一个基于firebase的应用程序,我对这种错误有点不熟悉。

下面是代码

FutureBuilder(
future: FirebaseFirestore.instance
.collection("userpreferences")
.doc(FirebaseAuth.instance.currentUser!.uid)
.get()
.then((value) {
return value.data()!["peopletohangoutwith"];
}),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data);
return Column(
children: [
const SizedBox(
height: 50,
),
Row(
children: [
Align(
alignment: Alignment.topLeft,
child: DelayedDisplay(
delay: const Duration(seconds: 1),
child: Padding(
padding: const EdgeInsets.only(left: 10),
child: IconButton(
icon: const Icon(
Icons.arrow_back_ios,
color: Colors.white,
),
onPressed: () {
Navigator.of(context).pop();
},
),
)),
),
const Align(
alignment: Alignment.topCenter,
child: DelayedDisplay(
delay: Duration(seconds: 1),
child: Text(
"Hang out with",
style: TextStyle(
fontSize: 26,
color: Colors.white,
fontFamily: "ProductSans",
fontWeight: FontWeight.bold),
),
),
),
],
),
const SizedBox(
height: 50,
),
const DelayedDisplay(
delay: Duration(seconds: 2),
child: Center(
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10),
child: Text(
"What type of people you want to hang out with",
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontFamily: "ProductSans",
fontWeight: FontWeight.bold),
),
),
),
),
const SizedBox(
height: 50,
),
DelayedDisplay(
delay: const Duration(seconds: 2),
child: Padding(
padding: const EdgeInsets.only(left: 30, right: 30),
child: TextFormField(
initialValue: snapshot.data["peopletohangoutwith"],
controller: _peopletohangoutwithController,
maxLines: 10,
decoration: InputDecoration(
hintText: "Write in as detail as possible",
fillColor: Colors.white,
filled: true,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(0),
borderSide: const BorderSide(
color: Colors.white,
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(0),
borderSide: const BorderSide(
color: Colors.white,
width: 2.0,
),
),
)),
),
),
const SizedBox(
height: 100,
),
DelayedDisplay(
delay: const Duration(seconds: 2),
child: Center(
child: FloatingActionButton.extended(
label: const Text('Save'),
backgroundColor: const Color(0xFF2A3B6A),
icon: const Icon(
Icons.save_as_outlined,
size: 24.0,
),
onPressed: () async {
if (_peopletohangoutwithController.text.isEmpty) {
Get.snackbar(
"Error",
"Please explain your preference",
colorText: Colors.white,
);
} else {
FirebaseFirestore.instance
.collection("userpreferences")
.doc(FirebaseAuth.instance.currentUser!.uid)
.set({
"peopletohangoutwith":
_peopletohangoutwithController.text,
});
}
},
),
),
),
],
);
},
),

此类型'String'不是'index'类型'int'的子类型,因为您正在尝试给字符串值而不是索引,您应该给索引字符串值。

snapshot.data[index][(string)]

不是

snapshot.data[(string)]

如果我在使用数据之前忘记检查数据是否存在,则会发生此错误。但以下检查

if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}

最新更新