skip to Main Content

I’m new to Flutter and now I have a small task about creating a list of users with add, delete and update functions. The problem is about my catalog screen, after I created a catalog model, the destinationScreen shows an error "1 positional argument expected by ‘ListUser.new’, but 0 found". I don’t understand why and how to fix it, pls help me ;-; appreciate a lot

Here are my code for the List User and Catalog Screen:

Code for List User:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ListUser extends StatefulWidget{
  const ListUser(ListView listView, {super.key});

  @override
  ListUserState createState() =>  ListUserState();
    // TODO: implement createState
}

class  ListUserState extends State<ListUser> {
  final List<String> title = <String>[
    "Trang1",
    'Trang2',
    'Trang3',
    'Trang4',
    'Trang5',
    'Trang6',
    'Trang7',
    'Trang8',
    'Trang9',
    'Trang10'
  ];

  final List<String> subtitle = <String>[
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: const Text("List User"),
            actions: const <Widget>[
              Icon(
                Icons.add,
              ),
              SizedBox(width: 30)

            ]
        ),

        body: ListView.builder(
            itemCount: title.length,
            itemBuilder: (BuildContext context, int index) {
              const SizedBox(height: 50,);
              Image.asset('assets/images/icon_Gmail.png',
                width: 100,);
              return Container(
                alignment: const Alignment(-0.5, 0.5),
                height: 100,

                child: Card(
                  child: ListTile(
                      title: Text("${title[index]}"),
                      subtitle: Text("${subtitle[index]}"),
                      leading: const CircleAvatar(
                        backgroundImage: AssetImage(
                            "assets/images/icon_Gmail.png"),
                      ),
                      trailing: Row(
                        mainAxisSize: MainAxisSize.min,

                        children: <Widget>[
                            Expanded(
                              child: IconButton(
                                onPressed: () {
                                  setState(() {
                                    title.removeAt(index);
                                    subtitle.removeAt(index);
                                  });
                                },
                                icon: Icon(Icons.edit),
                              ),
                            ),

                          const SizedBox(width: 10),
                          Expanded(
                            child: IconButton( onPressed: () {},
                                  icon: Icon(Icons.delete),
                                  color: Colors.red,
                                ),
                          ),

                        ],
                      )
                  ),
                ),
              );
            }
        )
    );
  }
}

My Catalog Screen:
enter image description here

mport 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:teacher_flutter_vncert/widget/listuser.dart';
import 'package:teacher_flutter_vncert/widget/login.dart';
import 'package:teacher_flutter_vncert/widget/scaffold.dart';
import 'package:teacher_flutter_vncert/widget/userform.dart';

class CatalogModel {
  final String widgetName;
  final Widget destinationScreen;

  const CatalogModel({required this.widgetName, required this.destinationScreen});




  static List<CatalogModel> toList(){
    return [
      const CatalogModel(widgetName : 'Scaffold' , destinationScreen: ScaffoldWidget()),
      const CatalogModel(widgetName: 'Log In', destinationScreen: LogIn()),
      const CatalogModel(widgetName: 'List User', destinationScreen: ListUser()),
      const CatalogModel(widgetName: 'User Form', destinationScreen: UserForm()),
    ];
  }
}

2

Answers


  1. The error indicates that ListUser requires a ListView as an argument, but you have defined ListUser without ListView.

    const CatalogModel(widgetName: 'List User', destinationScreen: ListUser()),
    

    By the way, I see there is no use of the ListView argument in ListUser, you can remove it from ListUser and try again.

    Login or Signup to reply.
  2. First solution:
    Remove ListView listView from the parameter, as it’s not being used.

     class ListUser extends StatefulWidget{
          const ListUser({Key? key}) : super(key: key);
        
      @override
      ListUserState createState() =>  ListUserState();
    }
    

    Second solution:
    Add an empty ListView() in the parameter and remove const. For further implementation, you can pass a dynamic list.

    static List<CatalogModel> toList(){
      return [
        const CatalogModel(widgetName: 'Scaffold', destinationScreen: ScaffoldWidget()),
        const CatalogModel(widgetName: 'Log In', destinationScreen: LogIn()),
        CatalogModel(widgetName: 'List User', destinationScreen: ListUser(ListView())),
        const CatalogModel(widgetName: 'User Form', destinationScreen: UserForm()),
      ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search