I’m struggling to make a redirection to my page for reinitializing a forgotten password.
i have a website, having the link “com-example-app://reinitialiser_mdp/token=dda8f861-fd89-492f-8d55-bf85e27743b0” on it (just for testing), and i want to redirect the user to the ReinitialisationPage, only if he click on a link that is valid and that contain a token (that is pre stored in my db).
but this code only open the app on the login page, despite having the token printed out :
Reloaded 14 of 1149 libraries in 1 410ms (compile: 172 ms, reload: 458 ms, reassemble: 568 ms).
I/flutter (18156): Received uri host: reinitialiser_mdp
I/flutter (18156): token : dda8f861-fd89-492f-8d55-bf85e27743b0
here is the main.dart :
void main() {
runApp(
ChangeNotifierProvider(
create: (_) => UserProvider(),
child: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? _token;
StreamSubscription<String?>? _linkSubscription;
@override
initState(){
super.initState();
_linkSubscription = linkStream.listen((String? linkStr) {
if (linkStr != null) {
Uri linkUri = Uri.parse(linkStr);
print('Received uri host: ${linkUri.host}');
if (linkUri.host == 'reinitialiser_mdp') {
final tokenUri = linkUri.queryParameters['token'];
if (tokenUri != null) {
print('token : $tokenUri');
setState(() {
_token = tokenUri;
});
} else {
_token = null;
}
}
}
});
}
@override
void dispose() {
_linkSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch().copyWith(primary: const Color(0xFF008EFE)),
useMaterial3: true,
),
debugShowCheckedModeBanner: false,
home: _token == null ? _buildHomePage() : ReinitialiserMdpPage(token: _token),
);
}
Widget _buildHomePage() {
final userProvider = Provider.of<UserProvider>(context);
if (userProvider.user == null) {
return const MyLoginPage();
} else {
return const MyHomePage();
}
}
}
and the reiMdp.dart :
void main() {
runApp(
ChangeNotifierProvider(
create: (_) => UserProvider(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch().copyWith(primary: const Color(0xFF008EFE)),
useMaterial3: true,
),
debugShowCheckedModeBanner: false,
home: const ReinitialiserMdpPage(),
);
}
}
class ReinitialiserMdpPage extends StatefulWidget {
final String? token;
const ReinitialiserMdpPage({super.key, this.token});
@override
ReinitialiserMdpPageState createState() => ReinitialiserMdpPageState();
}
class ReinitialiserMdpPageState extends State<ReinitialiserMdpPage> {
late String? token;
@override
void initState() {
super.initState();
token = widget.token;
}
Future<bool> checkIfTokenInDB(token) async {
if(token == null) {
return false;
}
final response = await http.get(Uri.parse('${ApiConfig.baseUrl}/token/ifTokenInDb/$token'));
if(response.statusCode == 200) {
if(response.body == 'true') {
print('token dans la bdd');
return true;
} else {
print('token pas trouvé dans la bdd');
return false;
}
} else {
throw Exception('Erreur de chargement');
}
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Page de réinitialisation du mot de passe'),
Text('Token: $token'),
],
),
);
}
}
i tried a lot of thing but even chatgpt doesn’t know what to do.
Abdourrahmane Belmadani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.