错误:位置参数太少:1必需,0给出



由于某种原因,当我尝试使用ChatsScreen时,它显示了这个错误(标题)。例如,在下面的代码中,ChatsScreen的括号被加了红色下划线。

int _selectedItem = 0;
final List<Widget> _page = [
Center(child: Text("Home Screen"),),
ChatsScreen(),
ProfileScreen(),
];

这是ChatsScreen的代码(代码中没有显示错误):


class ChatsScreen extends StatefulWidget {
static String routeName = "ChatsScreen";
UserModel user;
ChatsScreen(this.user);
@override
State<ChatsScreen> createState() => _ChatsScreenState();
}

class _ChatsScreenState extends State<ChatsScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Chats"
),
backgroundColor: Colors.orange.shade500,
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection("users").doc(widget.user.uid).collection("messages").snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if(snapshot.hasData){
if(snapshot.data.docs.length < 1) {
return Center(
child: Text(
"No Chats Available"
),
);
}
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
var friendId = snapshot.data.docs[index].id;
var lastMsg = snapshot.data.docs[index]["last_msg"];
return FutureBuilder(
future: FirebaseFirestore.instance.collection("users").doc(friendId).get(),
builder: (context, AsyncSnapshot asyncSnapshot) {
if(asyncSnapshot.hasData) {
var friend = asyncSnapshot.data;
return ListTile(
leading: ClipRRect(
borderRadius: BorderRadius.circular(80),
child: CachedNetworkImage(
imageUrl: friend["image"],
placeholder: (context, url)=>CircularProgressIndicator(),
errorWidget: (context, url, error)=>Icon(Icons.error),
height: 50,
),
),
title: Text(friend["name"]),
subtitle: Container(
child: Text(
"$lastMsg",
style: TextStyle(
color: Colors.grey,
overflow: TextOverflow.ellipsis,
),
),
),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context)=>ChatScreen(
currentUser: widget.user,
friendId: friend["uid"],
friendName: friend["name"],
friendImage: friend["image"],
),
),
);
}
);
}
return LinearProgressIndicator();
},
);
}
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.search),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context)=> SearchScreen(widget.user)));
},
),
);
}
}

我觉得ChatsScreen后面的括号里应该有什么东西,但是我不知道。我希望有人能理解我的问题,因为我确实不理解。

您必须像这样给用户作为参数ChatScreen(user);

应该是这样的,错误会消失

int _selectedItem = 0;
final List<Widget> _page = [
Center(child: Text("Home Screen"),),
ChatsScreen(user),
ProfileScreen(),
];

也改变ChatScreen的这一部分,像这样,

class ChatScreen extends StatefulWidget {
const ChatScreen(this.user, {Key? key}) : super(key: key);
static String routeName = "ChatsScreen";
final UserModel user;
@override
State<ChatScreen> createState() => _ChatScreenState();
}

ChatScreen类的构造函数,需要一个参数:

ChatsScreen(this.user);

因此,在创建实例时,您应该传入一个UserModel类型的参数。

如果你不想这样做,你应该修改构造函数,允许参数是可选的。

相关内容

  • 没有找到相关文章

最新更新