如何将数据从StatelessWidget传递到Statefulwidget?

时间:2020-07-10 08:49:05

标签: json flutter flutter-widget

我正在尝试将数据从StatelessWidget传递到StatefulWidget,但无法这样做。 尝试了各种选择,但没有成功

以下是StatelessWidget的代码

class ListFieldsForm2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => ListFieldFormBloc2(),
      child: Builder(
        builder: (context) {
          final formBloc = context.bloc<ListFieldFormBloc2>();

          return Theme(
            data: Theme.of(context).copyWith(
              inputDecorationTheme: InputDecorationTheme(
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20),
                ),
              ),
            ),
            child: Scaffold(
              resizeToAvoidBottomInset: false,

              floatingActionButton: FloatingActionButton(
                onPressed: formBloc.submit,
                child: Icon(Icons.send),
              ),
              body: FormBlocListener<ListFieldFormBloc2, String, String>(
                onSubmitting: (context, state) {
                  //LoadingDialog.show(context);
                },
                onSuccess: (context, state) {

                  int u;

                  String copyrights;

                  String name1;

                  var parsedData = json.decode(state.successResponse);
                  List members = parsedData['members'];
                  members.forEach((member){

                    name1 = member['firstName'];
                    
                    List<String> _firstNames = [];
                    _firstNames.add(member["firstName"]);


                  });



                  MyHomePage().steps = members;
                  Navigator.pop(context);
                 
                },
                onFailure: (context, state) {


                  Scaffold.of(context).showSnackBar(
                      SnackBar(content: Text(state.failureResponse)));
                },
                
              ),
            ),
          );
        },
      ),
    );
  }
}

我尝试了MyHomePage().steps = members;和“ MyHomePage(steps:Members);”,但在StatefulWidget中仍然收到空值

我应该如何传递数据?

4 个答案:

答案 0 :(得分:0)

在StatelessWidget中创建一个回调函数(它将返回您要传递的数据)并将其通过构造函数传递给另一个窗口小部件

答案 1 :(得分:0)

Mark Davids尝试以下操作:

Navigator.pop(context, members);

从您的推送代码中获取数据:

final members = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => ListFieldsForm2()),
    );

答案 2 :(得分:0)

尝试一下

首页

RaisedButton(
              child: const Text('Send Data'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => Testing(todos: "Hello",),
                  ),
                );
           
              },
            ),

第二页

class Testing extends StatefulWidget {
  String todos;
  Testing({Key key, @required this.todos}) : super(key: key);

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

class _TestingState extends State<Testing> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(child: Text(widget.todos)),
    );
  }
}

答案 3 :(得分:0)

这是您要达到的目标,使用回调函数为您创建了一个示例示例:

import 'package:flutter/material.dart';

void main() => runApp(HomePage());

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

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

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          child: SecondPage(
            callback: (value) {
              print(value);
            },
          ),
        ),
      ),
    );
  }
}

typedef void StringCallback(String val);

class SecondPage extends StatelessWidget {
  final StringCallback callback;
  const SecondPage({Key key, this.callback}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        callback('This is a sample text');
      },
      child: Center(
        child: Container(
          child: Text('press'),
        ),
      ),
    );
  }
}


只需退房,让我知道这是否是您想要的。