首页 > 解决方案 > NoSuchMethodError:在 null 上调用了方法“>”

问题描述

我尝试集成bubble_bottom_bar,如文档示例(https://pub.dev/packages/bubble_bottom_bar)和Github上的解释(https://github.com/westdabestdb/bubble_bottom_bar/issues/20)所示。但我仍然收到错误:NoSuchMethodError: The method '>' was called on null。尝试调用:>(0)

这是我的代码:

import 'package:flutter/material.dart';
import 'package:bubble_bottom_bar/bubble_bottom_bar.dart';
import 'package:project/pages/page.dart';
import 'package:project/pages/page1.dart';
import 'package:project/pages/page2.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int currentIndex;

  void changePage(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  Widget callPage(int _selectedBar) {
    switch (_selectedBar) {
      case 0:
        return HomePage();
      case 1:
        return pagePage();
      case 2:
        return page1Page();
      case 3:
        return page2Page();
        break;
      default:
        return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('MoneyTracker'),
      ),
      body: this.callPage(this.currentIndex),

      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
        backgroundColor: Colors.red,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      bottomNavigationBar: BubbleBottomBar(
        opacity: .2,
        currentIndex: currentIndex,
        onTap: changePage,
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
        elevation: 8,
        fabLocation: BubbleBottomBarFabLocation.end, //new
        hasNotch: true, //new
        hasInk: true, //new, gives a cute ink effect
        inkColor: Colors.black12, //optional, uses theme color if not specified
        items: <BubbleBottomBarItem>[
          BubbleBottomBarItem(
              backgroundColor: Colors.red,
              icon: Icon(
                Icons.dashboard,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.dashboard,
                color: Colors.red,
              ),
              title: Text("Home")),
          BubbleBottomBarItem(
              backgroundColor: Colors.deepPurple,
              icon: Icon(
                Icons.access_time,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.access_time,
                color: Colors.deepPurple,
              ),
              title: Text("Logs")),
          BubbleBottomBarItem(
              backgroundColor: Colors.indigo,
              icon: Icon(
                Icons.folder_open,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.folder_open,
                color: Colors.indigo,
              ),
              title: Text("Folders")),
          BubbleBottomBarItem(
              backgroundColor: Colors.green,
              icon: Icon(
                Icons.menu,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.menu,
                color: Colors.green,
              ),
              title: Text("Menu"))
        ],
      ),

标签: androidiosflutterdart

解决方案


看了一下BubbleBottomBar的文档,我发现了这行代码:

assert(0 <= currentIndex && currentIndex < items.length)

这个断言比较currentIndex变量。

在 Dart 中,你不能在 null 上调用方法。所以像这样的代码if(null > 3)会抛出一个错误

问题是,在您的代码中,您初始化 currentIndex 而不为其分配变量。

只需更改此:

class _HomePageState extends State<HomePage> {
  int currentIndex;

对此:

class _HomePageState extends State<HomePage> {
  int currentIndex = 0;

推荐阅读