android - 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"))
],
),
解决方案
看了一下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;
推荐阅读
- javascript - 在 client_credentials Spotify API 请求上获取 400 错误状态代码
- html - 为 Angluar SPA 的应用程序实现路由
- axios - 如何保证响应与请求的处理顺序相同
- spring-boot - 使用 Mockito 测试业务层应该怎么做?
- caching - 清除清漆后执行操作(清除另一台服务器)
- android - 适配器中的两个视图
- git - Gitlab将存储库主文件与文件夹同步?
- php - 在 JWT Token 创建过程中了解 RS256 和 SHA256
- reactjs - React 错误:只能在函数组件的主体内部调用挂钩
- kendo-ui - 基于保存的列列表的 KendoGrid 多列标题列重新排序