Home > Enterprise >  error: The argument type 'Null' can't be assigned to the parameter type 'Map<
error: The argument type 'Null' can't be assigned to the parameter type 'Map<

Time:11-16

I am writing my first Flutter App with some online tutorials and I found error that I can't fix it. I am trying to add Navigation by Navigator, but I can't understand why it doesn't work.

Once I am using Navigator in GestureDetector and it works fine, but I don't know what I supposed to do in floatingActionButton to make it work the same way. Note(NoteMode.Adding, null) probably should be something else instead null, because this null is making error (error from title). Can someone explain me what I am doing wrong and what I don't undarstand

Note List

  @override
  _NoteListState createState(){return _NoteListState();}
}

class _NoteListState extends State<NoteList> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Notes"),
      ),
      body: FutureBuilder(
        future: NoteProvider.getNoteList(),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            final notes = snapshot.data;
            return ListView.builder(
              itemBuilder: (context, index) {
                return GestureDetector(
                  onTap: () {
                    Navigator.push(
                        context, MaterialPageRoute(builder: (context) =>
                        Note(NoteMode.Editing, (notes as dynamic)[index]))
                    );
                  },
                  child: Card(
                    child: Padding(
                      padding: const EdgeInsets.only(
                          top: 30.0, bottom: 30.0, left: 13, right: 22),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          _NoteTitle((notes as dynamic)[index]['title']),
                          Container(height: 3,),
                          _NoteText((notes as dynamic)[index]['text']),
                        ],
                      ),
                    ),
                  ),
                );
              },
              itemCount: notes.length,
            );
          }
          return Center(child: CircularProgressIndicator());
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) => Note(NoteMode.Adding, null)));
        },
        child: Icon(Icons.add),
      ),

        );
    }

}

Note

enum NoteMode{
  Editing,
  Adding
}

class Note extends StatefulWidget{

  final NoteMode noteMode;
  final Map<String, dynamic> note;

  Note(this.noteMode, this.note,);

  @override
  State<Note> createState() => _NoteState();
}

class _NoteState extends State<Note> {
  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _textController = TextEditingController();

  List<Map<String, String>> get _notes => NoteInheritedWidget.of(context).notes;

  @override
  void didChangeDependencies(){
    if(widget.noteMode == NoteMode.Editing){
      _titleController.text = widget.note['title'];
      _textController.text = widget.note['text'];
    }
    super.didChangeDependencies();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.noteMode == NoteMode.Adding ? 'Add note' : 'Edit note',
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(40.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: _titleController,
              decoration: InputDecoration(
                hintText: "Note title",
              ),
            ),
            Container(height: 8,),
            TextField(
              controller: _textController,
              decoration: InputDecoration(
                hintText: "Note text",
              ),
            ),
            Container(height: 15,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                _NoteButton('SAVE', Colors.lightBlue, (){
                  final title = _titleController.text;
                  final text = _textController.text;
                  if(widget.noteMode == NoteMode.Adding){
                    NoteProvider.insertNote({
                      'title': title,
                      'text': text
                    });
                  } else if (widget.noteMode == NoteMode.Editing){
                    NoteProvider.updateNote( {
                      'id': widget.note['id'],
                      'title': _titleController.text,
                      'text': _textController.text,
                    });
                  }
                  Navigator.pop(context);}),
                _NoteButton('DISCARD', Colors.grey, (){Navigator.pop(context);}),
                widget.noteMode == NoteMode.Editing ?
                _NoteButton('DELETE', Colors.redAccent, () async {
                  await NoteProvider.deleteNote(widget.note['id']);
                  Navigator.pop(context);})
                    : Container(),
              ],
            )
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Either you have to pass Map in place of null because you are receiving a Map on that page

Navigator.push(context, MaterialPageRoute(builder: (context) => Note(NoteMode.Adding, {"key":"value"})));

or you have to make Map nullable as

class Note extends StatefulWidget{

  final NoteMode noteMode;
  final Map<String, dynamic>? note;

  Note(this.noteMode, this.note,);

  @override
  State<Note> createState() => _NoteState();
}
  • Related