![](/img/trans.png)
[英]The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'. Why I am getting this error in my listview builder?
[英]I am Getting The operator '[]' isn't defined for the type 'AsyncSnapshot<Object?>'. Try defining the operator '[]' error
嘿,我是 flutter 的新手,目前遇到一個錯誤,即當我返回用戶名時,我收到該方法未定義的錯誤。當我返回帶有用戶名的 textwidget 時,futurebuilder 中的構建器內部錯誤。 我添加了 null 安全檢查,但仍然沒有任何意義,因為它給了我一個錯誤。我想知道它為什么會這樣。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class messagebubble extends StatelessWidget {
// const messagebubble({Key? key}) : super(key: key);
messagebubble(this.message, this.isMe, this.userID, {required this.key});
final String message;
final bool isMe;
final Key key;
final String userID;
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment:
isMe ? MainAxisAlignment.end : MainAxisAlignment.start,
children: [
Container(
decoration: BoxDecoration(
color: isMe ? Colors.red : Colors.grey,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12),
bottomLeft: !isMe ? Radius.circular(0) : Radius.circular(12),
bottomRight: isMe ? Radius.circular(0) : Radius.circular(12),
),
),
width: 150,
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 16),
margin: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
child: Column(children: [
FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.doc(userID)
.get(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Text('Loading ...');
}
return Text(
snapshot.data!['username'],
style: TextStyle(fontWeight: FontWeight.bold),
);
}),
Text(
message,
style: TextStyle(
color:
isMe ? Theme.of(context).accentColor : Colors.purple),
),
]),
),
]);
}
}
將來嘗試使用.snapshot() 而不是.get()
future : FirebaseFirestore.instance
.collection('users')
.doc(userID)
.snapshots()
may it is works
嘗試使用這個
return Text(
snapshot.data!['username'] as String,
style: TextStyle(fontWeight: FontWeight.bold),);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.