Liam Hemphill
Liam Hemphill

Reputation: 75

The argument type 'Null' can't be assigned to the parameter type 'int'. (Flutter)

I am stuck on this one null error, I cannot fix this error that reads "error: The argument type 'Null' can't be assigned to the parameter type 'int'." The error shows up at the selected: null, line. I have added my code below hopefully someone can help. Let me know if any other code is needed.

floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () async {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => StudyPage(
                    title: 'Add a study',
                    selected: null,
                  )));

addition

import 'package:flutter/material.dart';
import 'package:timestudy_test/models/study.dart';
import 'package:timestudy_test/models/task.dart';
import 'package:timestudy_test/viewmodels/study_viewmodel.dart';

class StudyPage extends StatefulWidget {
  final String title;
  final int selected;

  StudyPage({required this.title, required this.selected});

  @override
  State createState() => StudyPageState();
}

class StudyPageState extends State<StudyPage> {
  late Study study;
  late TextField nameField;
  TextEditingController nameController = new TextEditingController();
  late TextField taskNameField;
  TextEditingController taskNameController = new TextEditingController();

  @override
  void initState() {
    nameField = new TextField(
      controller: nameController,
      decoration: InputDecoration(
          labelText: 'Study name'),
    );
    taskNameField = new TextField(
      controller: taskNameController,
      decoration:
      InputDecoration(labelText: 'Task name'),
    );
    if(widget.selected != null) {
      study = StudyViewModel.studies[widget.selected];
      nameController.text = study.name;
    } else {
      study = new Study(
          name: "",
          tasks: <Task>[]
      );
    }
    super.initState();
  }

  @override
  void dispose() {
    nameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Material(
            child: Padding(padding: EdgeInsets.all(16.0), child: Column(
              children: <Widget>[
                Padding(padding: EdgeInsets.only(bottom: 8.0), child: nameField),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text('Tasks:', style: TextStyle(fontSize: 18.0),),
                    IconButton(
                      icon: Icon(Icons.add),
                      onPressed: () async {
                        await showDialog(
                            context: context,
                            builder: (context) {
                              return AlertDialog(
                                title: Text('Add a task'),
                                content: taskNameField,
                                actions: <Widget>[
                                  FlatButton(
                                    child: Text('Cancel'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                  ),
                                  FlatButton(
                                    child: Text('Accept'),
                                    onPressed: () {
                                      if(taskNameController.text == ""){
                                        errorDialog(context, 'Please enter a task name!');
                                      } else {
                                        setState(() {
                                          study.tasks.add(new Task(
                                              name: taskNameController.text,
                                              elapsedTime:
                                              StudyViewModel.milliToElapsedString(
                                                  0)));
                                          taskNameController.clear();
                                        });
                                        Navigator.of(context).pop();
                                      }
                                    },
                                  ),
                                ],
                              );
                            });
                      },
                    )
                  ],
                ),
                Expanded(
                  child: ListView.builder(
                    itemCount: study.tasks.length,
                    itemBuilder: (context, int index) {
                      return ListTile(
                        title: Text(study.tasks[index].name),
                        trailing: IconButton(
                          icon: Icon(Icons.delete),
                          onPressed: () {
                            setState(() {
                              study.tasks.removeAt(index);
                            });
                          },
                        ),
                      );
                    },
                  ),
                ), Spacer(),
                Center(
                    child: RaisedButton(
                      color: Theme.of(context).accentColor,
                      child: Text('Save'),
                      onPressed: () async {
                        if (nameController.text == "") {
                          errorDialog(context, 'Please enter a study name!');
                        } else {
                          if (study.tasks.length < 1) {
                            errorDialog(context, 'Please add at least one task!');
                          } else {
                            study.name = nameController.text;
                            if (widget.selected != null) {
                              StudyViewModel.studies[widget.selected] = study;
                              await StudyViewModel.saveFile();
                              Navigator.of(context).pop();
                            } else {
                              if (StudyViewModel.checkName(nameController.text)) {
                                errorDialog(context, 'Study name already taken!');
                              } else {
                                StudyViewModel.studies.add(study);
                                await StudyViewModel.saveFile();
                                Navigator.of(context).pop();
                              }
                            }
                          }
                        }
                      },
                    ))
              ],
            ),
            )));
  }

  void errorDialog(BuildContext context, String message) async {
    await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            content: Text(message),
            actions: <Widget>[
              FlatButton(
                child: Text('Close'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        }
    );
  }
} 

Upvotes: 3

Views: 8725

Answers (3)

Ameer Hamza
Ameer Hamza

Reputation: 105

you cant assign a value null to not Null Variable

int selected; <------- it only allow the int value such as 0,1,2,3, and goes on

if you try to pass value to this page you can check the value either it is an integer or a null value

int? nullableInterger;

 Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => StudyPage(
                    title: 'Add a study',
                    selected: nullableInterger ?? 0,// here 0 is const value you can chose whatever you want
                  )));

Upvotes: 1

Joel
Joel

Reputation: 289

You have to specify the selected argument of StudyPage as nullable. To do so

edit this code:

class StudyPage extends StatefulWidget {
  final String title;
  final int selected;

  StudyPage({required this.title, required this.selected});

  @override
  State createState() => StudyPageState();
}

And change it to

class StudyPage extends StatefulWidget {
  final String title;
  final int? selected;

  StudyPage({required this.title, required this.selected});

  @override
  State createState() => StudyPageState();
}

Upvotes: 2

luckysmg
luckysmg

Reputation: 369

You can't pass null to a nonnull property

class StudyPage extends StatefulWidget {
  final String title;
  final int selected;
.....


change to 

class StudyPage extends StatefulWidget {
  final String title;
  final int? selected; <<<----- 

Upvotes: 1

Related Questions