Flutter Cupertino bottom_tab_bar:'方法'方法'在 null 上被调用'

Posted

技术标签:

【中文标题】Flutter Cupertino bottom_tab_bar:\'方法\'方法\'在 null 上被调用\'【英文标题】:Flutter Cupertino bottom_tab_bar: 'The method 'method' was called on null'Flutter Cupertino bottom_tab_bar:'方法'方法'在 null 上被调用' 【发布时间】:2020-06-12 11:34:58 【问题描述】:

我的应用上有一个 cupertino bottom_tab_bar,但每次我点击其中一个图标来更改页面时,调试控制台都会显示:

════════手势捕获的异常═════════════════════════════␕══ ════════

在 null 上调用了方法“jumpToPage”。

接收者:空

尝试调用:jumpToPage(2)

════════════════════════════════════════════════ ═════════════════════════════════

它不会改变页面。

我的飞镖代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttershare/pages/activity_feed.dart';
import 'package:fluttershare/pages/profile.dart';
import 'package:fluttershare/pages/search.dart';
import 'package:fluttershare/pages/timeline.dart';
import 'package:fluttershare/pages/upload.dart';
import 'package:google_sign_in/google_sign_in.dart';

final GoogleSignIn googleSignIn = GoogleSignIn();

class Home extends StatefulWidget 
  @override
  _HomeState createState() => _HomeState();


class _HomeState extends State<Home> 
  bool isAuth = false;
  PageController pageController;
  int pageIndex = 0;

  @override
  void initState() 
    super.initState();
    pageController = PageController();
    // Detects when user signed in
    googleSignIn.onCurrentUserChanged.listen((account) 
      handleSignIn(account);
    , onError: (err) 
      print('Error signing in: $err');
    );
    // Reauthenticate user when app is opened
    googleSignIn.signInSilently(suppressErrors: false).then((account) 
      handleSignIn(account);
    ).catchError((err) 
      print('Error signing in: $err');
    );
  

  handleSignIn(GoogleSignInAccount account) 
    if (account != null) 
      print('User signed in!: $account');
      setState(() 
        isAuth = true;
      );
     else 
      setState(() 
        isAuth = false;
      );
    
  

  @override
  void dispose() 
    pageController.dispose();
    super.dispose();
  

  login() 
    googleSignIn.signIn();
  

  logout() 
    googleSignIn.signOut();
  

  onPageChanged(int pageIndex) 
    setState(() 
      this.pageIndex = pageIndex;
    );
  

  onTap(int pageIndex) 
    pageController.jumpToPage(
      pageIndex,
    );
  

  Scaffold buildAuthScreen() 
    return Scaffold(
      body: PageView(
        children: <Widget>[
          Timeline(),
          ActivityFeed(),
          Upload(),
          Search(),
          Profile(),
        ],
        controller: pageController,
        onPageChanged: onPageChanged,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: CupertinoTabBar(
          currentIndex: pageIndex,
          onTap: onTap,
          activeColor: Theme.of(context).primaryColor,
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.whatshot)),
            BottomNavigationBarItem(icon: Icon(Icons.notifications_active)),
            BottomNavigationBarItem(
              icon: Icon(
                Icons.photo_camera,
                size: 35.0,
              ),
            ),
            BottomNavigationBarItem(icon: Icon(Icons.search)),
            BottomNavigationBarItem(icon: Icon(Icons.account_circle)),
          ]),
    );
    // return RaisedButton(
    //   child: Text('Logout'),
    //   onPressed: logout,
    // );
  

  Scaffold buildUnAuthScreen() 
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topRight,
            end: Alignment.bottomLeft,
            colors: [
              Theme.of(context).accentColor,
              Theme.of(context).primaryColor,
            ],
          ),
        ),
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(
              'FlutterShare',
              style: TextStyle(
                fontFamily: "Signatra",
                fontSize: 90.0,
                color: Colors.white,
              ),
            ),
            GestureDetector(
              onTap: login,
              child: Container(
                width: 260.0,
                height: 60.0,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(
                      'assets/images/google_signin_button.png',
                    ),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  

  @override
  Widget build(BuildContext context) 
    return isAuth ? buildAuthScreen() : buildUnAuthScreen();
  



颤振版本: 已经是最新的了。 Flutter 在频道测试版上已经是最新的了 Flutter 1.14.6 • 频道测试版 • https://github.com/flutter/flutter.git 框架 • 修订 fabeb2a16f(4 周前) • 2020-01-28 07:56:51 -0800 引擎 • 修订版 c4229bfbba 工具 • Dart 2.8.0(构建 2.8.0-dev.5.0 fc3af737c7)

【问题讨论】:

【参考方案1】:

首先,我建议不要创建一个名为onTap() 的方法来保持良好的实践。

在您的情况下,在您的情况下,当您调用 CupertinoTabBar 小部件的 onTap 属性中的方法时,您没有指定或提供 pageIndex 值。 这应该是正确的方法,也应该能解决问题

      bottomNavigationBar: CupertinoTabBar(
          currentIndex: pageIndex,
          onTap: onTap(pageIndex),
    //rest of your code

由于您没有提供pageIndex,因此它会将值视为空值并尝试对空值调用jumpToPage 方法。

【讨论】:

以上是关于Flutter Cupertino bottom_tab_bar:'方法'方法'在 null 上被调用'的主要内容,如果未能解决你的问题,请参考以下文章

07Flutter--Cupertino

Flutter之Cupertino (iOS风格) Widgets

Flutter:带有hintText的Cupertino TextField

获取 Flutter Cupertino_icons 依赖错误

Flutter 自定义组件实战之Cupertino(iOS)风格的复选框

Flutter Cupertino 文本字段大小错误