skip to Main Content
import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme!.primary,
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _showModalBottomSheet(context); // Pass context here
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

void _showModalBottomSheet(BuildContext context) {
  showModalBottomSheet(
    context: context,
    builder: (BuildContext context) {
      return Container(
        height: 200,
        color: Colors.white,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(
                'Modal Bottom Sheet',
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  Navigator.pop(context);
                  aboutnoteorfolder(
                    context,
                    arrowoperation: () {
                     
                      Navigator.push(context, MaterialPageRoute(
                        builder: (context) {
                          return V();
                        },
                      ));
                    },
                  );
                },
                child: Text('Next'),
              ),
            ],
          ),
        ),
      );
    },
  );
}

void aboutnoteorfolder(
  BuildContext context, {
  required Function() arrowoperation,
}) {
  final AnimationController controller = AnimationController(
    vsync: Navigator.of(context),
    reverseDuration: Duration(milliseconds: 250),
    duration: Duration(milliseconds: 700),
  );
  showModalBottomSheet(
      elevation: 0,
      transitionAnimationController: controller,
      useSafeArea: true,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      barrierColor: const Color.fromARGB(90, 0, 0, 0),
      context: context,
      builder: (context) {
        return Padding(
            padding: EdgeInsets.only(
                bottom: MediaQuery.of(context).size.width * 0.02),
            child: GestureDetector(
              onTap:arrowoperation,
              child: Container(
                width: MediaQuery.of(context).size.width * .955,
                height: MediaQuery.of(context).size.width * .65,
                decoration: BoxDecoration(
                  color: Color.fromARGB(255, 0, 128, 255),
                  border: Border.all(
                    width: MediaQuery.of(context).size.width * 0.0030,
                    color: Color.fromARGB(120, 255, 255, 255),
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(
                    MediaQuery.of(context).size.width * 0.11,
                  )),
                ),
                child: Center(
                  child: Text(
                    "Navigate"
                  ),
                ),
              ),
            ));
      });
}

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

  @override
  Widget build(BuildContext context) {
    return const Scaffold();
  }
}

**
My issue is that when the plus icon is pressed, a bottom sheet appears. Upon pressing the "Next" button in that bottom sheet, another blue sheet appears. When I press this blue sheet, I want to navigate to page V. However, I’m encountering the following error:

════════ Exception caught by gesture ═══════════════════════════════════════════
Null check operator used on a null value

**

2

Answers


  1. void _showModalBottomSheet(BuildContext context) {
      showModalBottomSheet(
        context: context,
        builder: (BuildContext ctx2) {
          return Container(
            height: 200,
            color: Colors.white,
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text(
                    'Modal Bottom Sheet',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () {
                      Navigator.pop(ctx2);
                      aboutnoteorfolder(
                        context,
                        arrowoperation: () {
                         
                          Navigator.push(context, MaterialPageRoute(
                            builder: (context) {
                              return V();
                            },
                          ));
                        },
                      );
                    },
                    child: Text('Next'),
                  ),
                ],
              ),
            ),
          );
        },
      );
    }
    

    replace the code,
    just set a diferent names for your buidlContext

    in this case i’ve renamed the showModalBottomSheet context to ctx2

    take a look at :https://api.flutter.dev/flutter/widgets/BuildContext-class.html

    Login or Signup to reply.
  2. The root cause for this issue is:

    • builder gives you a context for the current modal bottom sheet (1)
    • then you pop this bottom sheet (2) -> cause this context is unmounted since it belongs to the bottom sheet, but now the bottom sheet was dismissed
    • finally, you re-use this context, which is unmounted (3) -> cause exception

    Solution: you should use a different context instead of the bottom sheet context:

    • change the name of bottom sheet context to context2, so you code will using the context from params was passed to the function
    • or ignore it by using the syntax builder: (_) {...}

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search