![](/img/trans.png)
[英]Flutter The method '[]' can't be unconditionally invoked because the receiver can be 'null'
[英]Listview Firebase Flutter The method '[]' can't be unconditionally invoked because the receiver can be 'null'
我想在 listView 中显示来自 firebase 的数据
我只想显示我收藏的 id
但我的标题有问题:doc.data()["id"],
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class DemandeList extends StatelessWidget {
final db = FirebaseFirestore.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Demandes listes"),
centerTitle: true,
),
body: StreamBuilder<QuerySnapshot>(
stream: db.collection('demande').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else
return ListView(
children: snapshot.data!.docs.map((doc) {
return Card(
child: ListTile(
title: doc.data()["id"],
),
);
}).toList(),
);
},
),
);
}
}
尝试
doc.id;
// Or doc.Id;
代替
doc.data()['id'];
该问题与 Flutter 更新有关。
在最新的 Flutter 更新中,不需要添加 '.data()'。
从代码中删除“.data()”应该可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.