0
votes

Please help. I have categoryListItems function to build list view.

ListView categoryListItems() {
    return ListView.builder(
      shrinkWrap: true,
      padding: EdgeInsets.all(0),
      itemCount: this.count,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          dense: true,
          title: Text(
            this.cats[index].title,
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
          onTap: () => openAddCategoryDialog(this.cats[index]),
        );
      },
    );
  }

Also I have openAddCategoryDialog function with give argument for onTap method

    void openAddCategoryDialog(Category cat) async {
    Category save = await Navigator.of(context).push(
      new MaterialPageRoute<Category>(
        builder: (BuildContext context) {
          return new AddCategoryScreen(cat);
        },
        fullscreenDialog: true,
      ),
    );
    if (save != null) {
      setState(() {
        getData();
      });
    }
  }

Once debugging I get below error. The following _TypeError was thrown building LayoutScreen(dirty, state: _LayoutScreenState#35628): type '(Category) => void' is not a subtype of type '(() => void)?

1
try to remove void keyword for alert box function - Ravindra S. Patil
The error indicates that you have a function that takes a Category as a required positional argument, and you pass that function where a (optional) function that takes no argument is expected. However, I don't see that in the code you've shown. Are you sure that the error comes from this code and not from somewhere else? - jamesdlin
I just removed the void type from the function openAddCategoryDialog and it works fine. Thank you! - Otabek Ochilov

1 Answers

0
votes

When you are using Category save = ... means it will never return null. and push returns nullable Future object.

try Category? save = await Navigator.of(context).push(..

Demo


class Body extends StatefulWidget {
  const Body({Key? key}) : super(key: key);

  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  void openAddCategoryDialog(int cat) async {
    Category? save = await Navigator.of(context).push(
      new MaterialPageRoute<Category>(
        builder: (BuildContext context) {
          return Scaffold(
            body: Container(
              child: Text("value got $cat"),
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          );
        },
        fullscreenDialog: true,
      ),
    );
    if (save != null) {
      print(" got value ");
    } else
      print("got null");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(
        builder: (context, constraints) => ListView.builder(
          shrinkWrap: true,
          padding: EdgeInsets.all(0),
          itemCount: 4,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              dense: true,
              title: Text("title here"),
              onTap: () => openAddCategoryDialog(Random().nextInt(44)),
            );
          },
        ),
      ),
    );
  }
}