我正在开发一个带有漂移的应用程序。
一切正常,但我需要一些东西。
我需要检查当应用程序进入前台时用户的令牌是否已过期。
我通过以下方式获取应用状态
didChangeAppLifecycleState(AppLifecycleState state)
。
我的代码示例是这样的(我无法编写真实代码,因为它在我工作的计算机中,现在我在家里):
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
void didChangeAppLifecycleState(AppLifecycleState state) {
var isLogged = boolFunctionToCheckLogin();
if (state == AppLifecycleState.resume && isLogged) {
// THIS IS THE PLACE WHEN I TRY TO CODE THE MAGIC
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Tutorial Lifecycle'),
),
body: Center(),
);
}
但是,在didChangeAppLifecycleState函数内部:
所有这些都有相同的问题:context为null或相似。
如果我直接使用button或其他按钮执行任何这些选项,则可以使用。
但是当应用程序出现在前台时,系统会告诉我上下文为null或Material
任何人都可以帮助知道该怎么做?
谢谢大家!
答案 0 :(得分:0)
我已经解决了。
在带有“魔术”的条件标记中,我在setState内部调用了“ showDialog”:
class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def __repr__(self):
return repr(self.data)
class SinglyLinkedList:
def __init__(self):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.head = None
def __repr__(self):
"""
Return a string representation of the list.
Takes O(n) time.
"""
nodes = []
curr = self.head
while curr:
nodes.append(repr(curr))
curr = curr.next
return '[' + ', '.join(nodes) + ']'
def prepend(self, data):
"""
Insert a new element at the beginning of the list.
Takes O(1) time.
"""
self.head = ListNode(data=data, next=self.head)
def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
if not self.head:
self.head = ListNode(data=data)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = ListNode(data=data)
if __name__ == '__main__':
singly_linked_list = SinglyLinkedList()
print(singly_linked_list)
input_array = [-10, -3, 0, 5, 9]
for x in input_array:
print(x)
singly_linked_list.append(x)
print(singly_linked_list)