简体   繁体   English

手势捕获的异常:处理手势时引发以下 NoSuchMethodError:

[英]Exception caught by gesture : The following NoSuchMethodError was thrown while handling a gesture:

I'm a newbie to programming and Flutter.我是编程和 Flutter 的新手。

On my app, I've built a page navigator with a int variable.在我的应用程序中,我构建了一个带有 int 变量的页面导航器。 Upon change in the integer value, I would like the app to route the user to the specific designated page.在更改 integer 值后,我希望应用程序将用户路由到特定的指定页面。 However, I'm getting an 'anonymous closure' error onPressed.但是,我在按下时收到“匿名关闭”错误。

Would appreciate if someone could explain why I'm getting this error and post a rectified version of this code?如果有人能解释为什么我收到此错误并发布此代码的更正版本,将不胜感激?

Below is the full error snippet in the debugger.以下是调试器中的完整错误片段。

Reloaded 1 of 529 libraries in 885ms.
flutter: Page #1 Clicked

════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'call' was called on null.
Receiver: null
Tried calling: call()

When the exception was thrown, this was the stack
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _MyHomePageState.build.<anonymous closure>
package:navigator_test/main.dart:45
#2      _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:993
#3      _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:1111
#4      GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:183
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#5df0c
    debugOwner: GestureDetector
    state: possible
    won arena
    finalPosition: Offset(208.7, 509.7)
    finalLocalPosition: Offset(76.7, 19.7)
    button: 1
    sent tap down
════════════════════════════════════════════════════════════════════════════════

Below is the depiction of the Navigator下面是导航器的示意图

import 'package:flutter/material.dart';
import 'package:navigator_test/main.dart';
import 'package:navigator_test/page1.dart';

class Navigator extends StatefulWidget {
  @override
  _NavigatorState createState() => _NavigatorState();
}

class _NavigatorState extends State<Navigator> {
  int nav = 0;

  void page1() {
    setState(() {
      nav = 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (nav < 1) {
      return MyHomePage(page1: page1);
    } else if (nav < 2) {
      return Page1();
    } else {
      return null;
    }
  }
}

I'm importing the Function on the Main page.我正在主页上导入 Function。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final Function page1;
  MyHomePage({this.page1});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Navigation Test'),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SizedBox(height: 20),
            FlatButton.icon(
              onPressed: () {
                print('Page #1 Clicked');
                widget.page1();
              },
              icon: Icon(Icons.add_shopping_cart),
              label: Text('Goto Page #1'),
            )
          ],
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

You're getting this error because you're calling MyHomePage directly in MyApp, without passing the argument page1, so when you press 'Goto Page #1' a null function is called.您收到此错误是因为您直接在 MyApp 中调用MyHomePage ,而没有传递参数 page1,因此当您按下“转到页面 #1”时,会调用 null function。 To solve the problem you can instead just call Navigator in MyApp :要解决该问题,您可以改为在MyApp中调用 Navigator :

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Navigator(),
    );
  }
}

But the main way to navigate to a new page without needing to call setState is to use the Navigator class of Flutter, like this (Just remember to remove your Navigator class first, so the IDE will know that you're calling flutter's Navigator class) : But the main way to navigate to a new page without needing to call setState is to use the Navigator class of Flutter, like this (Just remember to remove your Navigator class first, so the IDE will know that you're calling flutter's Navigator class)

onPressed: () {
  print('Page #1 Clicked');
  //push the new route, but allowing you to go back to the previous one
  Navigator.of(context).push(

    //Default page transition of android
    MaterialPageRoute(
      builder: (context) => Page1(),
    ),
  );
},

Or:或者:

Navigator.of(context).push(
  //Default page transition of IOS
  CupertinoPageRoute(
    builder: (context) => Page1(),
  ),
);

For more info see the docs .有关更多信息,请参阅文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误,手势捕获异常,处理手势时抛出以下 _CastError:Null 检查运算符用于 null 值 - Flutter Error, Exception caught by gesture, The following _CastError was thrown while handling a gesture: Null check operator used on a null value Flutter 异常:处理手势时抛出以下 NoSuchMethodError:getter &#39;email&#39; 在 null 上被调用 - Flutter Exception : The following NoSuchMethodError was thrown while handling a gesture: The getter 'email' was called on null Flutter - 处理手势时引发以下 ArgumentError:无效参数 - Flutter - The following ArgumentError was thrown while handling a gesture: Invalid argument(s) 手势捕获的异常 - Exception caught by Gesture GestureDetector(手势捕获的异常) - GestureDetector (Exception caught by gesture) 处理手势时引发以下 _CastError:Null check operator used on a null value - The following _CastError was thrown while handling a gesture: Null check operator used on a null value 处理手势时引发了以下断言: Scaffold.of() 使用不包含 Scaffold 的上下文调用 - The following assertion was thrown while handling a gesture: Scaffold.of() called with a context that does not contain a Scaffold 处理手势时抛出以下断言:使用不包含导航器的上下文请求导航器操作 - The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator 手势捕获的异常:堆栈溢出 - Exception caught by gesture: Stack Overflow 错误:Flutter 中的“手势捕获异常” - ERROR: “Exception caught by gesture” in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM