导航到另一个页面并返回后,Flutter Isolate 不会执行 setState
Flutter Isolate doesn't go do setState after navigate to a other page and back
你好,我有一个带计数器的徽章,每当我启动我的 Android 警报管理器时,它应该增加计数器并更新 UI 实时。我用隔离来做到这一点。一切正常,但是当我转到新的页面路由然后返回时,不再执行 SetState。我的错误在哪里?
int _counter = 0;
String isolateName = 'isolate';
final ReceivePort port = ReceivePort();
static SendPort uiSendPort;
@override
void initState() {
super.initState();
AndroidAlarmManager.initialize();
IsolateNameServer.registerPortWithName(
port.sendPort,
isolateName,
);
port.listen((_) async => await _incrementCounter());
}
Future<void> _incrementCounter() async {
print('Increment counter!');
// Ensure we've loaded the updated count from the background isolate.
await prefs.reload();
if (!mounted) return;
setState(() {
print("geht in die SetState");
_counter++;
});
}
///
///This is in my Callback for my Android Alarm Manager...
final prefs = await SharedPreferences.getInstance();
int currentCount = prefs.getInt(countKey) ?? 0;
await prefs.setInt(countKey, currentCount + 1);
// This will be null if we're running in the background.
uiSendPort ??= IsolateNameServer.lookupPortByName('isolate');
uiSendPort?.send(null);
initState
返回后没有再次调用,返回后尝试强制执行你想要的功能
类似的东西
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => YourScreen№2(),
))
.then((value) {
Foo() <- your func
});
你好,我有一个带计数器的徽章,每当我启动我的 Android 警报管理器时,它应该增加计数器并更新 UI 实时。我用隔离来做到这一点。一切正常,但是当我转到新的页面路由然后返回时,不再执行 SetState。我的错误在哪里?
int _counter = 0;
String isolateName = 'isolate';
final ReceivePort port = ReceivePort();
static SendPort uiSendPort;
@override
void initState() {
super.initState();
AndroidAlarmManager.initialize();
IsolateNameServer.registerPortWithName(
port.sendPort,
isolateName,
);
port.listen((_) async => await _incrementCounter());
}
Future<void> _incrementCounter() async {
print('Increment counter!');
// Ensure we've loaded the updated count from the background isolate.
await prefs.reload();
if (!mounted) return;
setState(() {
print("geht in die SetState");
_counter++;
});
}
///
///This is in my Callback for my Android Alarm Manager...
final prefs = await SharedPreferences.getInstance();
int currentCount = prefs.getInt(countKey) ?? 0;
await prefs.setInt(countKey, currentCount + 1);
// This will be null if we're running in the background.
uiSendPort ??= IsolateNameServer.lookupPortByName('isolate');
uiSendPort?.send(null);
initState
返回后没有再次调用,返回后尝试强制执行你想要的功能
类似的东西
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => YourScreen№2(),
))
.then((value) {
Foo() <- your func
});