Search code examples
firebaseflutternullscreennavigator

Parameter is null instead of an id with ModalRoute.of()


I am developing a mobile App with Flutter and Firebase.

I am trying to use pushNamed() and hand over a parameter. (an id)

I don't know how i could solve my problem.

Here is my Code:

@override
  void didChangeDependencies() {
    if (_isInit) {
      print(ModalRoute.of(context).settings.arguments);
      final productId = ModalRoute.of(context).settings.arguments;
      if (productId != null) {
        _editedAngebot = Provider.of<Angebote>(context).findByID(productId);
        _initValues = {
          'titel': _editedAngebot.titel,
          'beschreibung': _editedAngebot.beschreibung,
          'semester': _editedAngebot.semester.toString(),
          'fach': _editedAngebot.fach,
          'abteilung': _editedAngebot.abteilung,
        };
      }
    }
    _isInit = false;
    super.didChangeDependencies();
  }

And the other class, where I set the parameter. My "Angebot" object only has a default constructor.

trailing: isAllowed()
            ? IconButton(
                icon: Icon(Icons.edit),
                onPressed: () {
                  Navigator.of(context).maybePop();
                  Navigator.of(context)
                      .pushNamed('/editAngebot', arguments: id);
                })

Why is my ID null?


Solution

  • Your Id is null because you are popping a page first then pushing new page .

    Use pushReplacementNamed()

    Here is a code sample

    import 'package:flutter/material.dart';
    
    final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: FirstPage(),
          routes:{
            Secondpage.routeName:(context)=>Secondpage(),
          }
        );
      }
    }
    
    class FirstPage extends StatelessWidget {
      final String id = '01';
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(child:ElevatedButton(
            child: Text('GoTo secondPage'),
            onPressed: (){
              Navigator.of(context).pushReplacementNamed(Secondpage.routeName,arguments: id);
            },
          ))
        );
      }
    }
    
    class Secondpage extends StatelessWidget {
      static const routeName = 'secondpage';
      @override
      Widget build(BuildContext context) {
        final data = ModalRoute.of(context).settings.arguments as String;
        return Scaffold(
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(child:Text('$data')),
              ElevatedButton(
            child: Text('GoTo FirstPage'),
            onPressed: (){
              Navigator.of(context).pop();
            },
          )
            ],
          )
        );
      }
    }