我的应用很简单。
我有一个主页,您可以按一个按钮打开一个新的MaterialPageRoute。
在这个新的页面路线中,我有一个基本的下拉列表。
问题是,当我点击下拉列表时,我收到此错误:
forgot_password_controller_spec.rb
# frozen_string_literal: true
require 'rails_helper'
describe ForgotPasswordController do
before do
User.create!(email: 'xyz@gmail.com',
password: 'pass',
password_confirmation: 'pass')
end
describe 'POST create' do
subject { post :create, params: params }
context 'when email is found' do
let(:params) do
{ data: { attributes: { email: 'xyz@gmail.com' } } }
end
it { is_expected.to have_http_status(200) }
end
context 'when email is not found' do
let(:params) do
{ data: { attributes: { email: 'xyz2@gmail.com' } } }
end
it { is_expected.to have_http_status(404) }
end
context 'when wrong params passed' do
let(:params) do
{ data: '' }
end
it { is_expected.to have_http_status(500) }
end
end
end
下拉列表是“FilterPage”类的成员。只有当我将FilterPage放在新的页面路由中时才会发生错误。例如,如果我使用新的FilterPage()作为主页,一切都很完美。
要复制的所有代码如下。谢谢你看看!
let(:user) { instance_double('user') }
let(:save_result) { true }
答案 0 :(得分:1)
要解决您的问题,请更改
class FilterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final GlobalKey<MyDropDownState> categoryKey = new .......
final MyDropDown categoryDropdown = new MyDropDown(key: categoryKey,.....
到
static final GlobalKey<MyDropDownState> categoryKey = new .....
final MyDropDown categoryDropdown = new MyDropDown(key: categoryKey,......
@override
Widget build(BuildContext context) {
也就是说,在构建方法之外提取可变声明,并将 categoryKey 设为“static”(添加静态因为只能在初始化程序中访问静态成员)
发生错误是因为您在构建方法中声明了变量“ categoryKey ”和“ categoryDropdown ”。当对 FilterPage 进行路由并且调用构建方法从而创建 categoryKey 的不同实例时,这会产生问题。
您在
中所做的打印也是如此“print(”key:“+ categoryKey.toString());”
改变之前:
key: [LabeledGlobalKey<MyDropDownState>#4ef43]
key: [LabeledGlobalKey<MyDropDownState>#61c88]
key: [LabeledGlobalKey<MyDropDownState>#4eb6e]
改变之后:
key: [LabeledGlobalKey<MyDropDownState>#ac21e]
key: [LabeledGlobalKey<MyDropDownState>#ac21e]
key: [LabeledGlobalKey<MyDropDownState>#ac21e]
如您所见,类别键在更改后获得相同的代码。
希望这有帮助!