Search code examples
flutterdartgetternosuchmethoderror

NoSuchMethodError : The getter 'title' was called on null. Receiver : null. Tried calling: title. //Anyone please


These are some of my todo_detail.dart code. Please help me. In the debug console it says

The relevant error-causing widget was
TodoDetail
lib/Screens/todo_list.dart:100
When the exception was thrown, this was the stack
#1      TodoDetailState.build
package:todo_list/Screens/todo_detail.dart:35



class TodoDetail extends StatefulWidget {
  TodoDetail(this.todo, this.appBarTitle);

  final String appBarTitle;
  final Todo todo;

  @override
  State<StatefulWidget> createState() {
    return TodoDetailState(this.todo, this.appBarTitle);
  }
}

class TodoDetailState extends State<TodoDetail> {
  DatabaseHelper helper = DatabaseHelper();

  String appBarTitle;
  Todo todo;

  TextEditingController titleController = TextEditingController();
  TextEditingController descriptionController = TextEditingController();

  TodoDetailState(this.todo, this.appBarTitle);

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = Theme.of(context).textTheme.headline6;
    final Todo todo = ModalRoute.of(context).settings.arguments;

    titleController.text = todo.title;
    descriptionController.text = todo.description,......

This is the screenshot of the error after built it on my physical device


Solution

  • Todo is passed as a Widget argument in the code above, hence, we could access it by:

    final Todo todo = widget.todo;
    

    Full code example:

    class TodoDetailState extends State<TodoDetail> {
      DatabaseHelper helper = DatabaseHelper();
    
      String appBarTitle;
      Todo todo;
    
      TextEditingController titleController = TextEditingController();
      TextEditingController descriptionController = TextEditingController();
    
      TodoDetailState(this.todo, this.appBarTitle);
    
      @override
      Widget build(BuildContext context) {
           TextStyle textStyle = Theme.of(context).textTheme.headline6;
           final Todo todo = widget.todo;
           titleController.text = todo.title;
           descriptionController.text = todo.description;
           return Container();
        }
    }