Flutter 导航错误:导航到页面 V 时对 null 值使用 Null 检查运算符

问题描述 投票:0回答:1
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();
  }
}

** 我的问题是,当按下加号图标时,会出现底部工作表。按底部工作表中的“下一步”按钮后,会出现另一个蓝色工作表。当我按下这张蓝色的纸时,我想导航到第 V 页。但是,我遇到了以下错误:

════════手势捕获异常═════════════════════════════ ════════ ══════ 对空值使用空检查运算符

**

flutter dart
1个回答
0
投票
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: () {
                  
                  aboutnoteorfolder(
                    context,
                    arrowoperation: () {
                  Navigator.pop(context);
                     
                      Navigator.push(context, MaterialPageRoute(
                        builder: (context) {
                          return V();
                        },
                      ));
                    },
                  );
                },
                child: Text('Next'),
              ),
            ],
          ),
        ),
      );
    },
  );
}

用此代码替换 _showModalBottomSheet

问题是你在 onPressed 方法的指令块之前弹出*上下文 这意味着当您尝试再次访问上下文时上下文不存在

© www.soinside.com 2019 - 2024. All rights reserved.