设置可变错误的状态;该小部件已被卸载,因此状态不再具有上下文(应被视为已删除)

发布于 2025-01-22 11:42:43 字数 8899 浏览 0 评论 0原文

我对颤动是新手的,我有一个页面,我可以在搜索fieldstone中进行两个输入,以不同的变量(_selectedyear and code> _SelectedSem ),并基于输入。我创建一个URL。 在创建两个搜索字段并运行它时,如果我在第一个字段之前输入第二个字段,我没有错误,但是如果我在第二个字段之前输入第一个字段,则此错误弹出。

这个小部件已被卸下,因此国家不再具有上下文(应该是 被认为已倒闭)。考虑在处置或使用安装的Getter期间取消任何活跃的工作以确定状态是否仍处于活动状态。

这是 screenshot 页面的:

这是代码。

class _TestrState extends State<Testr> {
  String _selectedYear;
  String _selectedSem;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        title: const Text(
          'Testr',
          style: TextStyle(
            color: Colors.black,
          ),
        ),
        flexibleSpace: Container(
          decoration: const BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.bottomRight,
                  colors: [Colors.green, Colors.limeAccent])),
        ),
      ),
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                height: MediaQuery.of(context).size.height * 0.64,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Padding(
                      padding: EdgeInsets.all(20.0),
                      child: Text(
                        'Select a Year',
                        style: TextStyle(fontSize: 16, color: Colors.blueGrey),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      margin: const EdgeInsets.symmetric(horizontal: 20),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.2),
                            blurRadius: 10,
                            offset: const Offset(0, 10),
                          ),
                        ],
                      ),
                      child: SearchField(
                        hint: 'Search for Year',
                        searchInputDecoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.blueGrey.shade200,
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              width: 2,
                              color: Colors.blue.withOpacity(0.8),
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        maxSuggestionsInViewPort: 4,
                        itemHeight: 50,
                        suggestionsDecoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                        ),
                        onTap: (value) {
                          setState(() {
                            _selectedYear = value;
                          });

                          if (kDebugMode) {
                            print(value);
                          }
                        },
                        suggestions: const [
                          'Year One',
                          'Year Two',
                          'Year Three',
                          'Year Four',
                        ],
                      ),
                    ),
                    const Padding(
                      padding: EdgeInsets.all(20.0),
                      child: Text(
                        'Select a Semester',
                        style: TextStyle(fontSize: 16, color: Colors.blueGrey),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      margin: const EdgeInsets.symmetric(horizontal: 20),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.2),
                            blurRadius: 10,
                            offset: const Offset(0, 10),
                          ),
                        ],
                      ),
                      child: SearchField(
                        hint: 'Search for Semester',
                        searchInputDecoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.blueGrey.shade200,
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              width: 2,
                              color: Colors.blue.withOpacity(0.8),
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        maxSuggestionsInViewPort: 4,
                        itemHeight: 50,
                        suggestionsDecoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                        ),
                        onTap: (value) {
                          setState(() {
                            _selectedSem = value;
                          });

                          if (kDebugMode) {
                            print(value);
                          }
                        },
                        suggestions: const [
                          'Afghanistan',
                          'Turkey',
                          'Germany',
                          'France',
                          'Italy',
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                height: 90,
                padding: const EdgeInsets.only(right: 20, left: 20, bottom: 20),
                decoration: const BoxDecoration(
                  color: Colors.white,
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    _selectedYear == null
                        ? const Text(
                            'Select a Year and a Semester to Continue',
                            style:
                                TextStyle(fontSize: 14, color: Colors.blueGrey),
                          )
                        : Text(_selectedYear + "->" + _selectedSem,
                            style: TextStyle(
                                fontSize: 16,
                                color: Colors.grey.shade800,
                                fontWeight: FontWeight.w600)),
                    MaterialButton(
                      onPressed: () {
                        CreateURL(_selectedYear, _selectedSem);
                      },
                      color: Colors.black,
                      minWidth: 50,
                      height: 50,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(50),
                      ),
                      padding: const EdgeInsets.all(0),
                      child: const Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.blueGrey,
                        size: 24,
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

我试图将固定状态放入“ IF(已安装)”中,并且在查看其他类似问题后不起作用。

I'm fairly new to Flutter and I have a page where I take in two inputs in a search fieldstone them in different variables(_selectedYear and _selectedSem) and based on the inputs I create a url.
On creating the two search fields and running it , if I input in the second field before the first one, I have no error but if I input in the first field before the second field this error pops up.

This widget has been unmounted, so the State no longer has a context (and should be
considered defunct). Consider canceling any active work during dispose or using the getter mounted to determine if the state is still active.

Here's a screenshot of the page:

Here is the code.

class _TestrState extends State<Testr> {
  String _selectedYear;
  String _selectedSem;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        title: const Text(
          'Testr',
          style: TextStyle(
            color: Colors.black,
          ),
        ),
        flexibleSpace: Container(
          decoration: const BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.bottomRight,
                  colors: [Colors.green, Colors.limeAccent])),
        ),
      ),
      body: Container(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                height: MediaQuery.of(context).size.height * 0.64,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Padding(
                      padding: EdgeInsets.all(20.0),
                      child: Text(
                        'Select a Year',
                        style: TextStyle(fontSize: 16, color: Colors.blueGrey),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      margin: const EdgeInsets.symmetric(horizontal: 20),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.2),
                            blurRadius: 10,
                            offset: const Offset(0, 10),
                          ),
                        ],
                      ),
                      child: SearchField(
                        hint: 'Search for Year',
                        searchInputDecoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.blueGrey.shade200,
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              width: 2,
                              color: Colors.blue.withOpacity(0.8),
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        maxSuggestionsInViewPort: 4,
                        itemHeight: 50,
                        suggestionsDecoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                        ),
                        onTap: (value) {
                          setState(() {
                            _selectedYear = value;
                          });

                          if (kDebugMode) {
                            print(value);
                          }
                        },
                        suggestions: const [
                          'Year One',
                          'Year Two',
                          'Year Three',
                          'Year Four',
                        ],
                      ),
                    ),
                    const Padding(
                      padding: EdgeInsets.all(20.0),
                      child: Text(
                        'Select a Semester',
                        style: TextStyle(fontSize: 16, color: Colors.blueGrey),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      margin: const EdgeInsets.symmetric(horizontal: 20),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.grey.withOpacity(0.2),
                            blurRadius: 10,
                            offset: const Offset(0, 10),
                          ),
                        ],
                      ),
                      child: SearchField(
                        hint: 'Search for Semester',
                        searchInputDecoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.blueGrey.shade200,
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              width: 2,
                              color: Colors.blue.withOpacity(0.8),
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        maxSuggestionsInViewPort: 4,
                        itemHeight: 50,
                        suggestionsDecoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10),
                        ),
                        onTap: (value) {
                          setState(() {
                            _selectedSem = value;
                          });

                          if (kDebugMode) {
                            print(value);
                          }
                        },
                        suggestions: const [
                          'Afghanistan',
                          'Turkey',
                          'Germany',
                          'France',
                          'Italy',
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                height: 90,
                padding: const EdgeInsets.only(right: 20, left: 20, bottom: 20),
                decoration: const BoxDecoration(
                  color: Colors.white,
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    _selectedYear == null
                        ? const Text(
                            'Select a Year and a Semester to Continue',
                            style:
                                TextStyle(fontSize: 14, color: Colors.blueGrey),
                          )
                        : Text(_selectedYear + "->" + _selectedSem,
                            style: TextStyle(
                                fontSize: 16,
                                color: Colors.grey.shade800,
                                fontWeight: FontWeight.w600)),
                    MaterialButton(
                      onPressed: () {
                        CreateURL(_selectedYear, _selectedSem);
                      },
                      color: Colors.black,
                      minWidth: 50,
                      height: 50,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(50),
                      ),
                      padding: const EdgeInsets.all(0),
                      child: const Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.blueGrey,
                        size: 24,
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Ive tried placing the setstate in an 'if(mounted)' and that didn't work after looking at other similar questions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2025-01-29 11:42:43

我刚刚更改了变量初始化,它起作用了。

String _selectedYear = "";
String _selectedSem = "";

还将此逻辑添加到setState中,

setState(() {
     _selectedSem = value!;
});

这对我有用,请尝试一下,看看

i just changed the variables initialization and it worked.

String _selectedYear = "";
String _selectedSem = "";

and also added this logic to the setState

setState(() {
     _selectedSem = value!;
});

this did the trick for me, try it out and see

千年*琉璃梦 2025-01-29 11:42:43

我不确定,但是一旦您尝试这种方式。首先,将值初始化为变量,然后刷新状态

_selectedYear = value!;
setState(() {});

,或者如果您使用的是2.5.3或更高版本,则需要初始化变量或迁移至无效安全。

I'm not sure but once you try this way. first, initialize value to a variable and then refresh state

_selectedYear = value!;
setState(() {});

or if you are using flutter version 2.5.3 or higher then you need to initialize variable or migrate to null safety.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文