gpt4 book ai didi

android - NoSuchMethodError : The method 'ancestorStateOfType' was called on null with await and async method

转载 作者:行者123 更新时间:2023-12-03 02:41:51 25 4
gpt4 key购买 nike

我想创建一个加载器数据,最后加载数据屏幕将自动更改屏幕(使用导航器)。

但我有一些问题。

Unhandled Exception: NoSuchMethodError: The method 'ancestorStateOfType' was called on null.

在“getDataOfUser()”方法结束时,“print(a)”执行得很好,但是当它试图改变屏幕时它崩溃了,我有这个错误:
E/flutter (32148): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'ancestorStateOfType' was called on null.
E/flutter (32148): Receiver: null
E/flutter (32148): Tried calling: ancestorStateOfType(Instance of 'TypeMatcher<NavigatorState>')
E/flutter (32148): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter (32148): #1 Navigator.of (package:flutter/src/widgets/navigator.dart:1446:19)
E/flutter (32148): #2 LoginScreenPresenter.initState.<anonymous closure> (package:test_app/login_presenter.dart:35:17)

class loginPresenter extends StatefulWidget {
Vendeur v;
loginPresenter({Key key, this.v}) : super(key: key);

@override
LoginScreenPresenter createState() => new LoginScreenPresenter();
}

class LoginScreenPresenter extends State<loginPresenter> {
RestDatasource api = new RestDatasource();

BuildContext context;

bool finish = false;

@override
void initState() {
getDataOfUser(widget.v).then((a) {
print(a)
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => HomePage(
v: widget.v,
)));
});

super.initState();
}

Future<bool> getDataOfUser(Vendeur user) async {

await api.getRegionsFromUser(user.idV).then((list) async {
if (list != null) {
for (var i = 0; i < list.length; ++i) {
await DBProvider.db.newRegion(list[i], 1);
}
}
});

await api.getClientsFromUser(user.idV).then((list) async {
if (list != null) {
for (var i = 0; i < list.length; ++i) {
await DBProvider.db.newClient(list[i], 1);
}
}
});

await api.getInterlocuteursFromUser(user.idV).then((list) async {
if (list != null) {
for (var i = 0; i < list.length; ++i) {
await DBProvider.db.newInterlocuteurs(list[i], 1);
}
}
});

await api.getVisitesFromUser(user.idV).then((list) async {
if (list != null) {
for (var i = 0; i < list.length; ++i) {
await DBProvider.db.newVisite(list[i], 1);
}
}
});

await api.getAvoirsFromUser(user.idV).then((list) async {
if (list != null) {
for (var i = 0; i < list.length; ++i) {
if (list[i].deleted == 0) {
await DBProvider.db.newAvoir(list[i], 1);
}
}
}
});

await api.getRapportsFromUser(user.idV).then((list) async {
if (list != null) {
for (var i = 0; i < list.length; ++i) {
await DBProvider.db.newRapport(list[i], user);
}
}
});

return true;
}

@override
Widget build(context) {
return RaisedButton(
onPressed: () {
Navigator.push(
context,
SlideRightRoute(
widget: HomePage(
v: widget.v,
)));
},
child: Text('go'),
);

}

}

最佳答案

我遇到了这个问题,我检查了 mounted ,现在它工作正常。

{...
if(!mounted) return;
Navigator.of(context).pop();
...}

关于android - NoSuchMethodError : The method 'ancestorStateOfType' was called on null with await and async method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56362432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com