我正在制作一个需要身份验证和令牌检查的应用程序。如果用户没有令牌,则需要呈现 Auth 页面;如果令牌不存在,则需要呈现 Home 。
我正在使用//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
// Get calendar set to the current date and time
Calendar cal = Calendar.getInstance();
// ensures we're using the same current time
Calendar cal2 = cal;
// Set time of calendar to 22:00:00.000
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
// Check if current time is after or before 22:00:00.000 today
if ((cal2.after(cal)) || (cal2.before(cal)) {
return;
}
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone="+ toNumber +"&text=" + URLEncoder.encode(whatsAppMessage, "UTF-8");
intent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, whatsAppImage);
intent.setType("image/jpeg");
intent.setPackage("com.whatsapp");
intent.setData(Uri.parse(url));
startActivity(intent);
}
catch (Exception e){
e.printStackTrace();
}
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
存储令牌。
颤振:1.5.4
flutter_secure_storage
我希望基于令牌获得import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:myapp/api.dart';
import 'package:myapp/router.dart';
class App extends StatelessWidget {
String _getInitialRoute(context) {
final hasToken = Provider.of<API>(context).token != null; // Returns Future<String> instead of string
return hasToken ? 'home' : 'auth';
}
// build async is throwing here an error.
build(context) {
return MaterialApp(
/**
* Disable debug banner.
*/
debugShowCheckedModeBanner: false,
/**
* Initial route.
*/
initialRoute: _getInitialRoute(context),
/**
* Routes.
*/
onGenerateRoute: Router.generateRoute,
/**
* Application title.
*/
title: 'My App',
);
}
}
。
答案 0 :(得分:0)
您可以尝试
void main() async {
String initialRoute = await SomethingHere(); // get your awaited string
runApp(
MaterialApp(
initialRoute: initialRoute,
onGenerateRoute: ...,
),
);
}
答案 1 :(得分:0)
尝试一下。
class MyApp extends StatelessWidget {
MyApp();
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp(
title: 'Chaco Digital',
debugShowCheckedModeBanner: false,
onGenerateRoute: generateRoute,
initialRoute: AUTH_HANDLER_ROUTE,
),
);
}
}
class AuthHandler extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new FutureBuilder(
future: _storage.read(key: 'token'),
builder: (_, AsyncSnapshot<bool> snapshot)
=> snapshot.hasData && snapshot.data ? HomePage() : SignInPage();
);
}
}