I'm new to flutter, I'm trying to allow my application once the user authentication is obtained to move to the DashboardPage, but I get this Error: Could not find the correct Provider above this DashboardPage Widget
This happens because you used a BuildContext
that does not include the provider
of your choice.
This is the code of my auth_page in both cases with code and biometric authentication must always lead to the DashboardPage () :
class AuthPage extends StatelessWidget {
static String routeName = '/fingerprint';
Future<void> localAuth(BuildContext context) async {
final localAuth = LocalAuthentication();
final didAuthenticate = await localAuth.authenticateWithBiometrics(
localizedReason: 'Please authenticate');
if (didAuthenticate) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DashboardPage()),
);
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
centerTitle: true,
),
body: Padding(
padding: EdgeInsets.all(32),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
buildPinScreen(context),
SizedBox(height: 24),
SizedBox(height: 24),
],
),
),
),
);
Widget buildPinScreen(BuildContext context) => buildButton(
text: 'UnLock',
icon: Icons.lock_open,
onClicked: () async {
screenLock(context: context, correctString: '1234',
canCancel: true,
didUnlocked: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DashboardPage()),
);
},
customizedButtonTap: () async {
await localAuth(context);
},
didOpened: () async {
await localAuth(context);
},
);
}
);
this is the code of my class route_configuration :
final router = FluroRouter();
class RouteConfiguration {
static Future<void> register() async {
// Register the routes
router.define("/", handler: rootHandler);
router.define(AssetFormScreen.routeName, handler: assetFormHandler);
router.define(AssetTransferScreen.routeName, handler: assetTransferHandler);
router.define(ShareAddressScreen.routeName, handler: shareAddressHandler);
router.define(DashboardPage.routeName, handler: dashboardHandler);
}
}
this is the code of my class route_handlers :
var rootHandler = Handler(
type: HandlerType.route,
handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
return MultiBlocProvider(
providers: [
BlocProvider<MainBloc>(
create: (_) => MainBloc(accountRepository: accountRepository),
),
BlocProvider<NavigationBloc>(
create: (_) => NavigationBloc(tabs[0]),
),
],
child: MainScreen(),
);
},
);
var assetTransferHandler = Handler(
handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
final asset = context?.settings?.arguments;
if (asset is! AlgorandStandardAsset) return null;
return BlocProvider(
create: (_) =>
AssetTransferBloc(accountRepository: accountRepository)..start(asset),
child: AssetTransferScreen(),
);
},
);
var assetFormHandler = Handler(
handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
return BlocProvider<AssetFormBloc>(
create: (_) => AssetFormBloc(accountRepository: accountRepository),
child: AssetFormScreen(),
);
},
);
var shareAddressHandler = Handler(
handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
final address = params['address']?[0] ?? '';
return BlocProvider<AssetFormBloc>(
create: (_) => AssetFormBloc(accountRepository: accountRepository),
child: ShareAddressScreen(
address: address,
),
);
},
);
var dashboardHandler = Handler(
handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
return BlocProvider<DashboardBloc>(
create: (_) => DashboardBloc(accountRepository: accountRepository),
child: DashboardPage(),
);
},
);
PLEASE HELP ME !
Bloc is not readily available to all the new pages that you push in the stack or even to new widgets. To provide existing bloc to new page, you have to use bloc provider.
Navigator.push(context,
MaterialPageRoute(
builder: (context1) =>
BlocProvider.value(
value: BlocProvider.of<BlocName>(context),
child: DashboardPage())));