问题
I have this codes to get access token which should pass from signin screen -> navscreen -> homescreen-> griddashboard->forms ->add new post and send the post request to server with the token
Here is how I get the token
Future<dynamic> handleGetTokenSilently() async {
String authority = "https://login.microsoftonline.com/$TENANT_ID";
final result = await msal.acquireTokenSilent([SCOPE], authority);
if(result !=null){
print('access token (truncated): ${result.accessToken}');
return result; //passing the result
}
else{
print('no access token');
return null;
}
}
and then passing it to the Navscreen
refreshSignedInStatus() async {
bool loggedIn = await msal.getSignedIn();
if (loggedIn) {
isSignedIn = loggedIn;
if(isSignedIn) {
dynamic data = await handleGetAccount();
dynamic token = await handleGetTokenSilently();
dynamic currentAccount = data;
dynamic result = token; <--- getting the token
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => NavScreen(
currentAccount : currentAccount, // <--- Passing required named parameter "currentAccount" in NavScreen widget.
result: result, <-- Passing required named parameter "result" in NavScreen widget
),
),
);
}
// Remaining code for navigation
}
}
then pass it to home screen
class NavScreen extends StatefulWidget {
const NavScreen({Key key, @required this.currentAccount, @required this.result}) : super(key: key);
@override
_NavScreenState createState() => _NavScreenState();
final dynamic currentAccount;
final dynamic result;
}
class _NavScreenState extends State<NavScreen> {
final List<Widget> _screens = [];
@override
void initState() {
_screens.add(Home(currentAccount: widget.currentAccount));
_screens.add(Home(result: widget.result));
then on the homescreen I have a gridview that has options to go to different pages.
how the gridview is called
SizedBox(
height: 20,
),
GridDashboard()
],
and then on the Griddashboard
class _GridDashboardState extends State<GridDashboard> {
Items home = new Items(screen: Home());
Items update = new Items(screen: CameraExampleHome());
Items bluetooth = new Items(screen: BluetoothApp());
Items forms = new Items(screen: FormsDashboard());
Items supervisor = new Items(screen: Home());
Items messages = new Items(screen: Home());
Items settings = new Items(screen: Settings());
Items check = new Items(screen: Home());
Items logout = new Items(screen: AudioRecorder());
how can I pass the token from the homescreen to the Griddashboard to FormsDashboard() and to post values to the server with the token?
回答1:
try to define global variables if u want that in entire app but it will get lost once the user close the app.
the best way is to store that token value in database like shared_preferences or 'hive` . Here is the doc of https://pub.dev/packages/shared_preferences and https://pub.dev/packages/hive resp
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('token', tokenvalue);
for getting bool value
bool counter = prefs.getString('token');
来源:https://stackoverflow.com/questions/64911301/how-to-pass-access-token-value-in-flutter