【问题标题】:How to validate and send the data to another page in flutter如何在颤动中验证数据并将其发送到另一个页面
【发布时间】:2021-12-04 20:49:34
【问题描述】:

我是个颤抖的新手 我想创建简单的输入数据并将数据传递到其他页面并显示文本。

这是我的代码

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const appTitle = 'Form Validation Demo';

    return MaterialApp(
      title: appTitle,
      home: Scaffold(
        appBar: AppBar(
          title: const Text(appTitle),
        ),
        body: const MyForm(),
      ),
    );
  }
}

class MyForm extends StatefulWidget {
  const MyForm({Key? key}) : super(key: key);

  @override
  MyFormState createState() {
    return MyFormState();
  }
}


class MyFormState extends State<MyForm> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {

    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextFormField(
            validator: (value) {
              if (value == null || value.isEmpty) {
                return 'Please enter some text';
              }
              return null;
            },
          ),
           ElevatedButton(
              onPressed: () {
                if (_formKey.currentState!.validate()) {
                  Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => Page2(_formKey)),
                  );

                }
              },
              child: const Text('Submit'),
            ),
        ],
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  Page2(final data){
    this.data = data;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 2'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(this.data),
            RaisedButton(
              child: Text('BACK'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );
  }
}

但最后,它得到了
没有为“Page2”类型定义设置器“数据”。 错误

知道怎么做吗?

我的代码出了什么问题

非常感谢。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以创建一个全局变量并在其他屏幕上轻松使用它。您所需要的只是将您的一些代码更新为以下内容

      final _formKey = GlobalKey<FormState>();
      TextEditingController inputController = TextEditingController();
      String result;
    
      class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        const appTitle = 'Form Validation Demo';
    
        return MaterialApp(
          title: appTitle,
          home: Scaffold(
            appBar: AppBar(
              title: const Text(appTitle),
            ),
            body: const MyForm(),
          ),
        );
      }
    }
    
    class MyForm extends StatefulWidget {
      const MyForm({Key? key}) : super(key: key);
    
      @override
      MyFormState createState() {
        return MyFormState();
      }
    }
    
    
    class MyFormState extends State<MyForm> {
      final _formKey = GlobalKey<FormState>();
    
      @override
      Widget build(BuildContext context) {
    
        return SafeArea(
      child: Scaffold(
        body: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextFormField(
                controller: inputController,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter some text';
                  }
                  return null;
                },
              ),
              ElevatedButton(
                onPressed: () {
                  if (_formKey.currentState.validate()) {
                    setState(() {
                      result = inputController.text;
                    });
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => Page2()),
                    );
    
                  }
                },
                child: const Text('Submit'),
              ),
            ],
          ),
        ),
      ),
    );
      }
    }
    
    class Page2 extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
      appBar: AppBar(
        title: Text('Page 2'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(result),
            RaisedButton(
              child: Text('BACK'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );
      }
    }
    

    【讨论】:

      【解决方案2】:

      您应该在 Page2 中定义data,如下所示:

      class Page2 extends StatelessWidget {
        final data = GlobalKey<FormState>();
      
        Page2(final data){
          this.data = data;
        }
      

      【讨论】:

        【解决方案3】:

        请参阅 Flutter 手册中的 Send data to a new screen

        【讨论】:

          猜你喜欢
          • 2021-10-23
          • 2021-04-15
          • 2020-11-12
          • 1970-01-01
          • 2021-08-21
          • 2021-02-26
          • 1970-01-01
          • 1970-01-01
          • 2021-01-30
          相关资源
          最近更新 更多