Home > OS >  Error: Too few positional arguments: 1 required, 0 given
Error: Too few positional arguments: 1 required, 0 given

Time:01-02

For some reason there is this error(Title) it shows when I try to use ChatsScreen. For Example in the following code the brackets of ChatsScreen are red underlined.

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

This is the code of the ChatsScreen (There is no error shown in this code):


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)));
        },
      ),
    );
  }
}

I feel like there's something meant to be in the brackets behind ChatsScreen but idk. I hope somebody understands my problem because I for sure don't.

CodePudding user response:

The constructor of your ChatScreen class, expects a parameter:

ChatsScreen(this.user);

So when creating an instance, you should pass in a parameter of UserModel type.

If that's not intended, you should change the constructor to allow the parameter being optional.

CodePudding user response:

You have to give user as parameter like this ChatScreen(user);.

It should look like this, the error will go away

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