RangeError(RangeError(index):无效值:有效值范围为空:0),带有flutter和dart



我正在尝试编写一个消息应用程序。当我试图点击用户导航到聊天屏幕时,我遇到了这个错误。完全错误:

The following RangeError was thrown while handling a gesture:
RangeError (index): Invalid value: Valid value range is empty: 0

这是我的源代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'chatrooms.dart';
class CommunicatePage extends StatefulWidget {
const CommunicatePage({Key? key}) : super(key: key);
@override
_CommunicatePageState createState() => _CommunicatePageState();
}
class _CommunicatePageState extends State<CommunicatePage>
with WidgetsBindingObserver {
Map<String, dynamic>? userMap;
bool isLoading = false;
final TextEditingController _search = TextEditingController();
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
setStatus("Online");
}
void setStatus(String status) async {
await _firestore.collection('students').doc(_auth.currentUser!.uid).update({
"status": status,
});
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
// online
setStatus("Online");
} else {
// offline
setStatus("Offline");
}
}
String chatRoomId(String user1, String user2) {
if (user1[0].toLowerCase().codeUnits[0] >
user2.toLowerCase().codeUnits[0]) {
return "$user1$user2";
} else {
return "$user2$user1";
}
}
void onSearch() async {
FirebaseFirestore _firestore = FirebaseFirestore.instance;
setState(() {
isLoading = true;
});
await _firestore
.collection('students')
.where("umail", isEqualTo: _search.text)
.get()
.then((value) {
setState(() {
userMap = value.docs[0].data();
isLoading = false;
});
print(userMap);
});
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: const Text("Chat Page"),
),
body: isLoading
? Center(
child: Container(
height: size.height / 20,
width: size.height / 20,
child: const CircularProgressIndicator(),
),
)
: Column(
children: [
SizedBox(
height: size.height / 20,
),
Container(
height: size.height / 14,
width: size.width,
alignment: Alignment.center,
child: Container(
height: size.height / 14,
width: size.width / 1.15,
child: TextField(
controller: _search,
decoration: InputDecoration(
hintText: "Search",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
),
SizedBox(
height: size.height / 50,
),
ElevatedButton(
onPressed: onSearch,
child: const Text("Search"),
),
SizedBox(
height: size.height / 30,
),
userMap != null
? ListTile(
onTap: () {
String roomId = chatRoomId(
_auth.currentUser!.displayName!,
userMap!['fname']);
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ChatRoom(
chatRoomId: roomId,
userMap: userMap!,
),
),
);
},
leading:
const Icon(Icons.account_box, color: Colors.black),
title: Text(
userMap!['fname'],
style: const TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.w500,
),
),
subtitle: Text(userMap!['umail']),
trailing: const Icon(Icons.chat, color: Colors.black),
)
: Container(),
],
),
// floatingActionButton: FloatingActionButton(
//   child: Icon(Icons.group),
//   onPressed: () => Navigator.of(context).push(
//     MaterialPageRoute(
//       builder: (_) => GroupChatHomeScreen(),
//     ),
//   ),
// ),
);
}
}

当我调试我的应用程序时,以下是它给我的错误。RangeError(RangeError((索引(:无效值:有效值范围为空:0(

您可能不会得到响应,因为在像一样分配之前最好检查其长度

.then((value) {
if(value.docs.length > 0)
{
setState(() {
userMap = value.docs[0].data();
isLoading = false;
});
}
});

最新更新