flutter - 收到错误“在构建期间调用 setState() 或 markNeedsBuild()”
问题描述
我在此 dart 文件中收到此错误“setState() 或 markNeedsBuild() 在构建期间调用”。在这里,我调用 Futurebuilder 来获取用户()身份验证信息,基本上我正在检查用户是否已登录,然后我将根据相应的屏幕移动到相应的屏幕。
我正在粘贴代码以供参考。谢谢。
import 'package:flutter/material.dart';
import 'package:mukti/ui_pages/login_signup/login_screen.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;
import 'package:mukti/ui_pages/main_screen/main_screen.dart';
import 'package:provider/provider.dart';
import 'authService.dart';
class CheckAuthentication extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: FutureBuilder(
future: Provider.of<AuthService>(context, listen: false).getUser(),
builder: (context, AsyncSnapshot<auth.User> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.error != null) {
print("error");
return Text(snapshot.error.toString());
}
return snapshot.hasData ? MainScreen(firebaseUser: snapshot.data) : LoginScreen();
} else {
return LoadingCircle();
}
}
)
);
}
}
class LoadingCircle extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).colorScheme.primaryVariant,
),
alignment: Alignment(0.0, 0.0),
),
);
}
}
供参考的异常消息
════════ Exception caught by foundation library ════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for AuthService:
setState() or markNeedsBuild() called during build.
This _InheritedProviderScope<AuthService> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _InheritedProviderScope<AuthService>
value: Instance of 'AuthService'
listening to value
The widget which was currently being built when the offending call was made was: CheckAuthentication
dirty
When the exception was thrown, this was the stack:
#0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4292:11)
#1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4307:6)
#2 _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:496:5)
#3 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:226:25)
#4 AuthService.getUser (package:mukti/authentication/authService.dart:18:7)
...
The AuthService sending notification was: Instance of 'AuthService'
════════════════════════════════════════════════════════════════════════════════════════════════════
解决方案
你的问题是调用notifyListeners()
你的getUser()
函数。删除该行,错误就会消失。这会尝试构建小部件树,而 FutureBuilder 正在构建中
推荐阅读
- android - java.net.SocketTimeoutException 在 1s~3s 后发生,但我的 http 超时设置为 10s
- c# - 来自客户端的集线器方法未在 ASP.NET Core SignalR 中调用
- android - 如何使用 react-native-modal-datetime-picker 获取多个计时并显示它
- angular - 无法在Angular 2的html文件中使用*ngFor遍历我的界面元素
- ruby - Is there a way to use a matcher like 'Verify' in rspec?
- python - 对于 find 命令,Python 子进程没有按预期工作
- embedded - 为什么在 UART RX FIFO 中提供 FIFO 四分之一满、半满、四分之三满中断?他们的用例是什么?
- mysql - mysql query on an audit table to get latest row where no delete row exists for each user and type
- objective-c - 如何在 Objective C / async 问题中解决这种竞争条件?'索引 1 超出空数组的范围'
- c# - 在 C# ASP.Net MVC 中更新子列表时出现问题