Search code examples
flutterwidgetnavigatorstatelessstateful

Navigator.pop() - How to pass `context` to be read by the navigator -


I'm following a flutter tutorial ,,

and in this widget , to make it disappear after entering data I'll call the navigator class ,, but in the tutorial I'll have to convert the stateless to stateful widget , althought I don't need it .

Haw can I use : Navigator.of(context).pop(); with my stateless widget without getting error that context isn't defined ? - How to pass context to be read by the navigator

CODE :

import 'package:flutter/material.dart';


class NewTransaction extends StatelessWidget {
  final titleController = TextEditingController();
  final amountController = TextEditingController();
  final Function addNEW;
  NewTransaction(this.addNEW);

  void submitData() {

if (titleController.text.isEmpty || double.parse(amountController.text) <= 0 ){

  return;
}
    Navigator.of().pop();

    addNEW(
      titleController.text,
      double.parse(amountController.text),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 5,
      child: Container(
        padding: EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            TextField(
              decoration: InputDecoration(labelText: 'Title'),
              controller: titleController,
              onSubmitted: (_) => submitData(),
            ),
            TextField(
              decoration: InputDecoration(labelText: 'Amount'),
              keyboardType: TextInputType.numberWithOptions(decimal: true),
              controller: amountController,
              onSubmitted: (_) => submitData(),
            ),
            FlatButton(
              child: Text('Add Transaction'),
              onPressed: submitData,
            )
          ],
        ),
      ),
    );
  }
}

Solution

  • When using a stateless widget, you can't access Context outside of the build method. To solve the error, you can do one of the following: 1) Convert your Stateless widget to a Stateful widget

    OR

    2) Pass the context as a parameter to your submitdata function. Like the code below:

    
      void submitData(BuildContext context) {
    
    if (titleController.text.isEmpty || double.parse(amountController.text) <= 0 ){
    
      return;
    }
        Navigator.of().pop();
    
        addNEW(
          titleController.text,
          double.parse(amountController.text),
        );
      }
    

    So when using it in the FlatButton, it would be something like below:

    onPressed: submitdata(context),
    

    3) Put the body of your submitdata function directly in the onPressed of you button.

    Like the code below:

    onPressed: (){
    if (titleController.text.isEmpty || double.parse(amountController.text) <= 0 ){
    
      return;
    }
        Navigator.of().pop();
    
        addNEW(
          titleController.text,
          double.parse(amountController.text),
        );
    }
    

    I hope this helps