How to connect parameter from function to a call to action in UI?

Viewed 27

Hello I try to make full migraion in nullsafty, but now I have an error between my function and the call of my function in the UI

Before upgrade all this code worked great Here is my old function

    void goToNewItemView() async {

    final slider= await showDialog<String>(
      context: context,
      builder: (context) => NewTodo(),
    ).then((title){
      if(title != null) {
        addItem(Todo_bis(title: title));
        _saveData();
      }
    }
   );
  } 

Here is my UI

             Align(
              alignment: Alignment.bottomCenter,
              child :  new RawMaterialButton(
                onPressed: () {
                  goToNewItemView();
                },
                child: new Icon(
                  Icons.add,
                  color:  Color(0xffff9a7b),
                  size: 30.0,
                ),
                shape: new CircleBorder(),
                elevation: 4.0,
                fillColor: Colors.white,
                padding: const EdgeInsets.all(10.0),
              ),
            )

Here is the State full Widget call by the function goToNewItemView() and call by showDialog

class NewTodo extends StatefulWidget {
  final Todo item;

  NewTodo({this.item });

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

class _NewTodoState extends State<NewTodo> {
...
 }

after flutter upgrade I have somes errors First I add required this.item to be compatible nullsafety in NewTodo statefull Widget After I have error of missing argument on my function goToNewItemView() so I add goToNewItemView(item) and NewTodo(item:item), But after that I have error in the UI section goToNewItemView(); need an arguement but I do'nt know what ?

If I let with no argument goToNewItemView(); I have error : 1 positional argument(s) expected, but 0 found

I don't know what argument add, I tried

goToNewItemView(item);
goToNewItemView(item: item);

but always error

Here is my function after upgrade

    void goToNewItemView(item) async {

    final slider= await showDialog<String>(
      context: context,
      builder: (context) => NewTodo(item:item),
    ).then((title){
      if(title != null) {
        addItem(Todo_bis(title: title));
        _saveData();
      }
    }
   );
  } 

Here is my UI

             Align(
              alignment: Alignment.bottomCenter,
              child :  new RawMaterialButton(
                onPressed: () {
                  goToNewItemView();
                },
                child: new Icon(
                  Icons.add,
                  color:  Color(0xffff9a7b),
                  size: 30.0,
                ),
                shape: new CircleBorder(),
                elevation: 4.0,
                fillColor: Colors.white,
                padding: const EdgeInsets.all(10.0),
              ),
            )

Here is the State full Widget call by the function goToNewItemView(item) and call by showDialog

class NewTodo extends StatefulWidget {
  final Todo item;

  NewTodo({required this.item });

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

class _NewTodoState extends State<NewTodo> {
...
 }

I found the soution, just convert final Todo item; to final Todo? item; like that I can remove required from required this.item and all the reste no need parameters

1 Answers

The error is not relative to null safety.

The parameter is not optional, so you need to specify it:

goToNewItemView(the_item_to_pass) 

If it doesn't work then there is another error. Or maybe it refers to these functions:

    addItem(Todo_bis(title: title));
    _saveData();
Related