首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >_LoginScreenState.build.<anonymous closure>

_LoginScreenState.build.<anonymous closure>
EN

Stack Overflow用户
提问于 2022-02-22 06:56:39
回答 1查看 1.4K关注 0票数 0

有人能帮我吗?运行代码时会出现错误。

这个完整的代码错误

代码语言:javascript
复制
Restarted application in 5,728ms.
D/EGL_emulation(12613): app_time_stats: avg=500967.44ms min=177.41ms max=1001757.50ms count=2
D/EGL_emulation(12613): app_time_stats: avg=1551.20ms min=1551.20ms max=1551.20ms count=1
E/flutter (12613): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: setState() called after dispose(): _LoginScreenState#96e43(lifecycle state: defunct, not mounted)
E/flutter (12613): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (12613): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (12613): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter (12613): #0      State.setState.<anonymous closure>
E/flutter (12613): #1      State.setState
E/flutter (12613): #2      _LoginScreenState.build.<anonymous closure>
E/flutter (12613): #3      _LoginScreenState.build.<anonymous closure>
E/flutter (12613): #4      _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter (12613): #5      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (12613): <asynchronous suspension>
E/flutter (12613):

这个登录页

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:login/api_service.dart';
import 'package:login/otp_verify.dart';
import 'package:login/login_page.dart';
import 'package:snippet_coder_utils/FormHelper.dart';


class LoginScreen extends StatefulWidget {
  
  

  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  TextEditingController patientNoController = TextEditingController();
  bool isAPICallProcess = false;

  String patientPhoneNumber = '+60176713856';
  
  @override
  Widget build(BuildContext context) {
    final patientNoController = TextEditingController();
     
    final logo = Hero(
      tag: 'logo',
      child: CircleAvatar(
        backgroundColor: Colors.transparent,
        radius: 40.0,
        child: Image.network(
          "https://i.imgur.com/bOCEVJg.png",
          height: 180,
          fit: BoxFit.contain,
          
        ),
      ),
    ); 
    final txtPatientNo = TextField(
      controller: patientNoController,
      decoration: InputDecoration(
        hintText: 'Phone Number',
        contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(30.0)
        )
        ),
       
    );
    
    final btnLogin = RaisedButton(
      child: Text("Verify"),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
      onPressed: (){
        if (patientNoController != null && patientNoController.text =="+60176713856");
          

              });
              APIService.signin(patientPhoneNumber).then((respone) async {
                setState(() {
                  isAPICallProcess = false;
                });
               if (respone) {
                 Navigator.pushAndRemoveUntil(
                   context, 
                   MaterialPageRoute(
                     builder: (context) => OtpPage(
                       
                     ),
                   ), 
                   
                   (route) => false,
                   
                );
               }

      },
    );

    return Scaffold(
      appBar: AppBar(
        title: Text("Patient Login Page"),
      ),
      body: Center(
        child: ListView(
          shrinkWrap: true,
          padding: EdgeInsets.only(left: 25, right: 25),
          children: <Widget>[
          logo,
          SizedBox(height: 20,),
          txtPatientNo,
          SizedBox(height: 20,),
          btnLogin,
        ],
        ),
      ),
    );

  }
}

有人能告诉我我的错误在哪里吗?我刚开始使用颤栗。我想使用TextEditingControl并创建一个APICallProcess。但我的问题是。我把第一页叠起来。我的编码对不对。有人可以向我解释这样我才能学到东西。

EN

回答 1

Stack Overflow用户

发布于 2022-02-22 07:38:11

这可能是因为在您调用setState之后,您立即调用了Navigator.pushAndRemoveUntilsetState将执行传递的回调(在本例中为() {isAPICallProcess = false;} ),并将其标记为要重建的小部件。

但是您的Navigator调用可以非常快地执行,所以当引擎试图在setState之后重新构建您的登录页面时,它就不再在小部件树中,而是被释放了。

我不知道您使用这个isAPICallProcess是什么,但我的猜测是,在这个小部件被释放之后,您并不真正需要它。因此,您可以使用此方法避免此错误消息,检查小部件的挂载状态:

代码语言:javascript
复制
if (mounted) {
  setState(() {
    isAPICallProcess = false;
  });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71217060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档