Home > OS >  flutter/dart Undefined name 'context'. Try correcting the name to one that is defined, or
flutter/dart Undefined name 'context'. Try correcting the name to one that is defined, or

Time:01-10

I have done IconButton in my main.dart appBar and I want to Navigate using this Icon to my second page named OneDice. When im trying to push it by using

Navigator.of(context).push(MaterialPageRoute(builder: (_) => OneDice(),),);

it doesnt work and I have no idea why.... Can u help me plis? The problem is with (context). Error name is in the topic. And here is mine code. Thanks for all!

import 'package:dicee/OneDice.dart';
import 'package:flutter/material.dart';
import 'dart:math';

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.green.shade300,
        body: const DicePage(),
        appBar: AppBar(
          title: const Center(child: Text('Dicee')),
          backgroundColor: Colors.purple,
          elevation: 10.0,
          actions: [
            IconButton(
              onPressed: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (_) => OneDice(),),);
              },
              icon: const Icon(
                Icons.indeterminate_check_box,
                size: 40,
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

class DicePage extends StatefulWidget {
  const DicePage({Key? key}) : super(key: key);

  @override
  State<DicePage> createState() => _DicePageState();
}

class _DicePageState extends State<DicePage> {
  int leftDiceNumber = 1;
  int rightDiceNumber = 3;

  void rollDice() {
    setState(() {
      leftDiceNumber = Random().nextInt(6)   1;
      rightDiceNumber = Random().nextInt(6)   1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Expanded(
            child: TextButton(
              onPressed: () {
                // when i will press dice it calls Widget build to rebuild
                rollDice();
              },
              child: Image.asset('images/dice$leftDiceNumber.png'),
            ),
          ),
          Expanded(
            child: TextButton(
              onPressed: () {
                rollDice();
              },
              child: Image.asset('images/dice$rightDiceNumber.png'),
            ),
          ),
        ],
      ),
    );
  }
}

CodePudding user response:

You can only access context in the build method. You could do it like this.

void main() {
  return runApp(
    MaterialApp(
      home:MyWidget(),
    ),
  );
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.green.shade300,
        body: const DicePage(),
        appBar: AppBar(
          title: const Center(child: Text('Dicee')),
          backgroundColor: Colors.purple,
          elevation: 10.0,
          actions: [
            IconButton(
              onPressed: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (_) => OneDice(),),);
              },
              icon: const Icon(
                Icons.indeterminate_check_box,
                size: 40,
              ),
            ),
          ],
        ),
      ),
  }
}

class DicePage extends StatefulWidget {
  const DicePage({Key? key}) : super(key: key);

  @override
  State<DicePage> createState() => _DicePageState();
}

class _DicePageState extends State<DicePage> {
  int leftDiceNumber = 1;
  int rightDiceNumber = 3;

  void rollDice() {
    setState(() {
      leftDiceNumber = Random().nextInt(6)   1;
      rightDiceNumber = Random().nextInt(6)   1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Expanded(
            child: TextButton(
              onPressed: () {
                // when i will press dice it calls Widget build to rebuild
                rollDice();
              },
              child: Image.asset('images/dice$leftDiceNumber.png'),
            ),
          ),
          Expanded(
            child: TextButton(
              onPressed: () {
                rollDice();
              },
              child: Image.asset('images/dice$rightDiceNumber.png'),
            ),
          ),
        ],
      ),
    );
  }
}

CodePudding user response:

You do your piece of code at void main which is totally wrong and the issue here is with Navigator not present in the parent context. You are using a context for the DiceApp which isn't under the navigator. Your working code as below.

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
  title: 'Welcome to Flutter',
  home: Scaffold(
    appBar: AppBar(
      title: Center(child: Text('Dicee')),
       actions: [
        IconButton(
          onPressed: () {
            Navigator.of(context)
                .push(MaterialPageRoute(builder: (_) => OneDice(),),);
          },
          icon: const Icon(
            Icons.indeterminate_check_box,
            size: 40,
          ),
        ),
      ],
    ),
  ),
);
}
}

CodePudding user response:

Just Use

Navigator.push(
 context,
 MaterialPageRoute(builder: (context) => OneDice()),);

And you can also use this code with GetX package

Get.to(() =>  OneDice());
  •  Tags:  
  • Related