在 Flutter 中从 main() 调用 setState()
Call setState() from main() in Flutter
我如何在我的 main() 中调用每个 Cron-Job 之后调用特定 State-Class 的 setState() 方法?
主要():
void main() async {
new Cron().schedule(new Schedule.parse('* * * * *'), () async {
uploadDocuments();
});
runApp(MaterialApp(
home: MainMenu(),
));
}
Class,我希望调用 setState() 的地方:
class DbObjectsDetails extends StatefulWidget
@override
_DbObjectsDetailsState createState() => _DbObjectsDetailsState();
}
class _DbObjectsDetailsState extends State<DbObjectsDetails> {
void initState() {
loadFilesFromDatabase(true);
}
}
您只能在有状态小部件中使用状态。主要功能不是小部件,而是飞镖功能。所以你无法在主函数中使用 setState() 函数。
可以尝试使用get_it
包注册一个GlobalKey
的单例,然后在两处使用
此代码可能有效:
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
GetIt locator = GetIt.instance..allowReassignment = true;
void setupLocator() {
locator.registerLazySingleton(() => GlobalKey<DbObjectsDetailsState>());
}
GlobalKey<DbObjectsDetailsState> newWidgetKey() {
locator.registerSingleton(GlobalKey<DbObjectsDetailsState>());
return locator<GlobalKey<DbObjectsDetailsState>>();
}
void main() async {
setupLocator()
new Cron().schedule(new Schedule.parse('* * * * *'), () async {
//you can acces the current state of the widget like that:
locator<GlobalKey<DbObjectsDetailsState>>().currentState?.rebuild();});
runApp(MaterialApp(
home: DbObjectsDetails(key: newWidgetKey()),
));
}
class DbObjectsDetails extends StatefulWidget {
DbObjectsDetails({Key key}) : super(key: key);
@override
DbObjectsDetailsState createState() => DbObjectsDetailsState();
}
class DbObjectsDetailsState extends State<DbObjectsDetails> {
@override
Widget build(BuildContext context) {
throw UnimplementedError();
}
void rebuild() => setState((){})
}
我如何在我的 main() 中调用每个 Cron-Job 之后调用特定 State-Class 的 setState() 方法?
主要():
void main() async {
new Cron().schedule(new Schedule.parse('* * * * *'), () async {
uploadDocuments();
});
runApp(MaterialApp(
home: MainMenu(),
));
}
Class,我希望调用 setState() 的地方:
class DbObjectsDetails extends StatefulWidget
@override
_DbObjectsDetailsState createState() => _DbObjectsDetailsState();
}
class _DbObjectsDetailsState extends State<DbObjectsDetails> {
void initState() {
loadFilesFromDatabase(true);
}
}
您只能在有状态小部件中使用状态。主要功能不是小部件,而是飞镖功能。所以你无法在主函数中使用 setState() 函数。
可以尝试使用get_it
包注册一个GlobalKey
的单例,然后在两处使用
此代码可能有效:
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
GetIt locator = GetIt.instance..allowReassignment = true;
void setupLocator() {
locator.registerLazySingleton(() => GlobalKey<DbObjectsDetailsState>());
}
GlobalKey<DbObjectsDetailsState> newWidgetKey() {
locator.registerSingleton(GlobalKey<DbObjectsDetailsState>());
return locator<GlobalKey<DbObjectsDetailsState>>();
}
void main() async {
setupLocator()
new Cron().schedule(new Schedule.parse('* * * * *'), () async {
//you can acces the current state of the widget like that:
locator<GlobalKey<DbObjectsDetailsState>>().currentState?.rebuild();});
runApp(MaterialApp(
home: DbObjectsDetails(key: newWidgetKey()),
));
}
class DbObjectsDetails extends StatefulWidget {
DbObjectsDetails({Key key}) : super(key: key);
@override
DbObjectsDetailsState createState() => DbObjectsDetailsState();
}
class DbObjectsDetailsState extends State<DbObjectsDetails> {
@override
Widget build(BuildContext context) {
throw UnimplementedError();
}
void rebuild() => setState((){})
}