NoSuchMethodError:方法“>”在null上被调用

时间:2020-08-14 18:47:17

标签: android ios flutter dart

我试图集成bubble_bottom_bar,如文档示例(https://pub.dev/packages/bubble_bottom_bar)和有关Github的说明(https://github.com/westdabestdb/bubble_bottom_bar/issues/20)所示。但是仍然出现错误:NoSuchMethodError:方法'>'在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"))
        ],
      ),

2 个答案:

答案 0 :(得分:0)

看看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;

答案 1 :(得分:0)

@Wilson Wilson @Krish Bhanushali @Gabe

我在currentIndex = 0中更改了currentIndex;但是现在在刷新应用程序时出现此错误,并且模拟器屏幕完全变成白色。

错误(终端)

Performing hot restart...                                               
(This is taking an unexpectedly long time.)       /E/flutter ( 6432): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 2624 pos 20: '_debugCurrentBuildTarget == context': is not true.
E/flutter ( 6432): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter ( 6432): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter ( 6432): #2      BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2624:20)
E/flutter ( 6432): #3      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2628:12)
E/flutter ( 6432): #4      RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1086:13)
E/flutter ( 6432): #5      WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:927:7)
E/flutter ( 6432): #6      WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:908:7)
E/flutter ( 6432): #7      _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 6432): #8      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 6432): #9      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 6432): #10     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 6432): #11     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 6432): #12     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 6432): #13     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter ( 6432): #14     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 6432): #15     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter ( 6432): #16     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter ( 6432): #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter ( 6432): 

Restarted application in 7.503ms.