Flutter ProviderNotFoundException with BLoC



我想做一个简单的导航从我的WelcomeScreen到我的NavigationScreen。当按钮"让我进去"时被按下,用户应该到达NavigationScreen,但是当按下&;providernotfoundexception&;被抛出。我不知道,这事跟集团有没有关系。到其他小部件的导航工作正常。我在这里做错了什么?

WelcomeScreen.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:speeddatingapp/UI/Welcome/WelcomePages/Welcome_1.dart';
import 'package:speeddatingapp/UI/Welcome/WelcomePages/Welcome_2.dart';
import 'package:speeddatingapp/UI/Welcome/WelcomePages/Welcome_3.dart';
import 'package:speeddatingapp/UI/Welcome/WelcomePages/Welcome_4.dart';
import 'package:speeddatingapp/domain/entities/classRoutes.dart';
import 'package:speeddatingapp/homemenu/landingPage.dart';
import 'package:speeddatingapp/homemenu/navigation.dart';
import 'bloc/welcome_bloc.dart';
class WelcomePage extends StatelessWidget {
WelcomePage({Key? key}) : super(key: key);
static String route = "Welcome";
final PageController _controller = PageController();
@override
Widget build(BuildContext context) {
return BlocBuilder<WelcomeBloc, WelcomeState>(
bloc: WelcomeBloc(),
builder: (context, state) {
return Scaffold(
body: Stack(
children: [
PageView(
physics: const ClampingScrollPhysics(),
controller: _controller,
children: const [
WelcomeOne(),
WelcomeTwo(),
WelcomeThree(),
WelcomeFour()
],
),
Container(
alignment: const Alignment(0, 0.75),
child: SmoothPageIndicator(
controller: _controller,
count: 4,
),
),
Container(
alignment: Alignment(0, 0.90),
child: CupertinoButton(
color: Colors.grey[700],
child: Text("Get me in !"),
onPressed: () => Navigator.popAndPushNamed(
context, NavigationScreen.route),
),
)
],
),
);
},
);
}
}
NavigationScreen.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:speeddatingapp/UI/Options/SettingsPageUI.dart';
import 'package:speeddatingapp/homemenu/cubit/bottom_navigation_cubit.dart';
import 'package:speeddatingapp/homemenu/home_screen_new.dart';
import 'package:speeddatingapp/homemenu/navbar_items.dart';
import 'package:speeddatingapp/navigator/profileScreen.dart';
int _selectedIndex = 0;
class NavigationScreen extends StatelessWidget {
const NavigationScreen({Key? key}) : super(key: key);
static String route = "Navigator";
void _onTappedItem(index) {
_selectedIndex = index;
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar:
BlocBuilder<BottomNavigationCubit, BottomNavigationState>(
builder: (context, state) {
return BottomNavigationBar(
currentIndex: state.selectedIndex,
type: BottomNavigationBarType.fixed,
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
BottomNavigationBarItem(
icon: Icon(Icons.person), label: "Profile"),
BottomNavigationBarItem(icon: Icon(Icons.chat), label: "Chat"),
BottomNavigationBarItem(
icon: Icon(Icons.settings), label: "Settings"),
],
onTap:
// select different BottomNavigationBarItems
(index) {
if (index == 0) {
BlocProvider.of<BottomNavigationCubit>(context)
.getNavBar(NavbarItem.home);
} else if (index == 1) {
BlocProvider.of<BottomNavigationCubit>(context)
.getNavBar(NavbarItem.profile);
} else if (index == 2) {
BlocProvider.of<BottomNavigationCubit>(context)
.getNavBar(NavbarItem.chat);
} else if (index == 3) {
BlocProvider.of<BottomNavigationCubit>(context)
.getNavBar(NavbarItem.settings);
}
},
);
},
),
body: BlocBuilder<BottomNavigationCubit, BottomNavigationState>(
builder: (context, state) {
if (state.navbarItem == NavbarItem.home) {
return const HomeScreenNew();
} else if (state.navbarItem == NavbarItem.profile) {
return const ProfileScreen();
} else if (state.navbarItem == NavbarItem.chat) {
return const Placeholder();
} else if (state.navbarItem == NavbarItem.settings) {
return const SettingPage();
}
return Container();
},
),
);
}
}
GetRoutes
getRoutes() {
return {
Login.route: (context) => const Login(),
Register.route: (context) => const Register(),
ForgotPassword.route: (context) => const ForgotPassword(),
TermsOfServicePopUp.route: (context) => const TermsOfServicePopUp(),
ProfileScreen.route: (context) => const ProfileScreen(),
LandingPage.route: (context) => const LandingPage(),
Optionmenu.route: (context) => const Optionmenu(),
SettingPage.route: (context) => const SettingPage(),
Preferences.route: (context) => const Preferences(),
NavigationScreen.route: (context) => const NavigationScreen(),
WelcomePage.route: (context) =>  WelcomePage()
};
}
main.dart
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:speeddatingapp/UI/Login/loginContainerBody.dart';
import 'package:speeddatingapp/UI/Login/loginMain.dart';
import 'package:speeddatingapp/UI/Welcome/WelcomePage.dart';
import 'package:speeddatingapp/UI/Welcome/bloc/welcome_bloc.dart';
import 'package:speeddatingapp/domain/entities/classSizeConfig.dart';
import 'package:speeddatingapp/homemenu/cubit/bloc/home_bloc.dart';
import 'package:speeddatingapp/homemenu/cubit/bottom_navigation_cubit.dart';
import 'package:speeddatingapp/homemenu/navigation.dart';
import 'package:speeddatingapp/injection.dart' as di;
import 'package:speeddatingapp/navigator/bloc/profile_screen_bloc.dart';
import 'package:speeddatingapp/navigator/profileScreen.dart';
import 'domain/entities/classRoutes.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await di.init();
runApp(const SpeedDatingApp());
}
class SpeedDatingApp extends StatelessWidget {
const SpeedDatingApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(fontFamily: 'Roboto'),
home: Builder(
builder: (BuildContext context) {
SizeConfig().initState(context);
return MultiBlocProvider(providers: [
BlocProvider<ProfileScreenBloc>(
create: (BuildContext context) => ProfileScreenBloc(),
),
BlocProvider<BottomNavigationCubit>(
create: (BuildContext context) => BottomNavigationCubit()),
BlocProvider<HomeBloc>(
create: (BuildContext context) => HomeBloc(),
),
BlocProvider<WelcomeBloc>(
create: (BuildContext context) => WelcomeBloc())
], child: WelcomePage());
},
),
routes: getRoutes(),
);
//});
}
}

修复了在Material App前创建Multiblocprovider的错误:

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await di.init();
runApp(const SpeedDatingApp());

}
class SpeedDatingApp extends StatelessWidget {
const SpeedDatingApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
SizeConfig().initState(context);
return MultiBlocProvider( //Before Material App
providers: [
BlocProvider<ProfileScreenBloc>(
create: (BuildContext context) => ProfileScreenBloc(),
),
BlocProvider<BottomNavigationCubit>(
create: (BuildContext context) => BottomNavigationCubit()),
BlocProvider<HomeBloc>(
create: (BuildContext context) => HomeBloc(),
),
BlocProvider<WelcomeBloc>(
create: (BuildContext context) => WelcomeBloc())
],
child: MaterialApp(

debugShowCheckedModeBanner: false,
theme: ThemeData(fontFamily: 'Roboto'),
home: Login(),
routes: getRoutes(),
));
//});
}
}

相关内容

  • 没有找到相关文章

最新更新