问题
I had an another issue before using 'Provider' package with 'PersistentBottomNavBar' package in my flutter app. So after solving that issue, immediately I got another one. There is my source code. When I run this code my Android emulator crushes. With the following error message: The method 'map' was called on null. Receiver: null. Tried calling: map(Closure: (PersistentBottomNavBarItem) => Flexible)
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';
import './screens/products_list_screen.dart';
import './screens/add_product_screen.dart';
import './screens/profile_screen.dart';
import './providers/products.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => Products(),
),
],
child: MaterialApp(
title: 'Sample App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blueGrey,
accentColor: Colors.grey[700],
),
home: MyApp(),
),
),
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
PersistentTabController _tabController =
PersistentTabController(initialIndex: 0);
bool _hideNavBar = false;
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_tabController.dispose();
}
List<Widget> _buildScreens() {
return [ProductsListScreen(), AddProductScreen(), ProfileScreen()];
}
List<PersistentBottomNavBarItem> _navBarItems() {
return [
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.list_bullet),
title: 'Products',
activeColor: CupertinoColors.activeBlue,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.add),
title: 'Add',
activeColor: CupertinoColors.activeBlue,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(CupertinoIcons.profile_circled),
title: 'Profile',
activeColor: CupertinoColors.activeBlue,
inactiveColor: CupertinoColors.systemGrey,
),
];
}
@override
Widget build(BuildContext context) {
return PersistentTabView(
context,
controller: _tabController,
screens: _buildScreens(),
items: _navBarItems(),
);
}
}
There is the error in my Android Emulator:
And the Debug console error in my VCode:
回答1:
The issue seems to be that you are not passing the navBarStyle to PersistentTabView. There are various NavBarStyle's to select and each of them have other values that they require. Please refer to 'persistent_bottom_nav_bar' documentation for more information. Kindly make the following changes to your code and check. Note NavBarStyle.style7 might not be the navBarStyle you were looking for, so please try others.
.....
return Scaffold(
body: PersistentTabView(
context,
controller: _tabController,
screens: _buildScreens(),
items: _navBarsItems(),
navBarStyle: NavBarStyle.style7,
),
....
来源:https://stackoverflow.com/questions/65278583/flutter-something-is-wrong-with-persistentbottomnavbar-package